WCF的Web API服务的路线与普通asp.net mvc的路线发生冲突(网络API preVIEW 4) [英] Wcf Web Api service routes conflicting with regular asp.net mvc routes (web api preview 4)

查看:112
本文介绍了WCF的Web API服务的路线与普通asp.net mvc的路线发生冲突(网络API preVIEW 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的MVC应用程序在使用时产生错误的URL:

I noticed that my mvc app is creating wrong url's when using:

@using (Html.BeginForm("Test", "Test"))
{
    <input type="submit" value="Submit" />
}

这是生成的HTML源代码:

This is the generated html source:

<form action="/books?action=Test&amp;controller=Test" method="post">

注意,行动开始的 /书籍。这是错误的!

我已经注意到的是,Html.BeginForm总是包括已登记MapServiceRoute第一腹板API的开头。 (见code以下)

What I've noticed is that Html.BeginForm is always including the beginning of the first web api which has been registered with MapServiceRoute. (see code below)

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var builder = HttpHostConfiguration.Create();
    routes.MapServiceRoute<BooksService>("books", builder);

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

我花了很长时间试图找出为什么网址在我的应用程序被打破,但毫无进展。
然后,我决定有着非常简单的MVC应用程序,果然这个问题可以很容易地复制到测试。
因为它可以得到的,是由ASP MVP创建我测试的MVC应用程序一样简单。你可以找到它的此处。我所做的只是添加和的TestController视图/测试/ Index.cshtml视图。在视图我添加上面显示的Html.BeginForm。如果你启动应用程序和访问测试控制器可以看到URL是错误的,只需将鼠标悬停提交按钮(或者看html源代码)。

I spent a long time trying to figure out why the url's were broken in my app, but got nowhere. I then decided to test with an extremly simple mvc app and sure enough the problem can be easily reproduced. The mvc app I tested this on is as simple as it can get, was created by an asp mvp. You can find it here. All I did was add the TestController and the Views/Test/Index.cshtml view. In the view I added the Html.BeginForm shown above. If you launch the app and visit the test controller you can see the url is wrong by just hovering the mouse over the submit button (or look at the html source).

任何人知道如何避免这个问题?

Anyone know how to avoid this problem?

修改
这适用于网页API preVIEW 4(2011年4月)。

EDIT: This applies for web api preview 4 (april 2011).

推荐答案

另一种方法是定义一个路由约束:

another way is to define a route constraint:

public class WcfRoutesConstraint : IRouteConstraint {
    public WcfRoutesConstraint(params string[] values) {
        this._values = values;
    }

    private string[] _values;

    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection) {

    // Get the value called "parameterName" from the
    // RouteValueDictionary called "value"
    string value = values[parameterName].ToString();

    // Return true is the list of allowed values contains
    // this value.
    bool match = !_values.Contains(value);
    return match;
    }
}

分配约束的MVC路线

Assigning the constraint to the MVC routes

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new { controller = new WcfRoutesConstraint(new string[] {"contact","login"}) }
    );

从接触的URL/登录和/接触

This prevents MVC from touching the Urls "/login" and "/contact"

这篇关于WCF的Web API服务的路线与普通asp.net mvc的路线发生冲突(网络API preVIEW 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆