Wcf Web Api 服务路由与常规 asp.net mvc 路由冲突(Web api 预览版 4)

发布于 2024-12-01 17:58:34 字数 1407 浏览 1 评论 0原文

我注意到我的 mvc 应用程序在使用时创建了错误的 url:

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

这是生成的 html 源:

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

请注意,操作以 /books 开头。这是错误的!

我注意到 Html.BeginForm 始终包含已在 MapServiceRoute 中注册的第一个 Web api 的开头。 (参见下面的代码)

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 应用程序进行测试,果然问题可以很容易地重现。 我测试的 mvc 应用程序非常简单,是由 asp mvp 创建的。您可以找到它 此处。我所做的只是添加 TestController 和 Views/Test/Index.cshtml 视图。在视图中,我添加了上面所示的 Html.BeginForm。如果您启动应用程序并访问测试控制器,只需将鼠标悬停在提交按钮上(或查看 html 源代码),您就会发现 url 是错误的。

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

编辑: 这适用于 Web API 预览版 4(2011 年 4 月)。

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

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

This is the generated html source:

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

Notice that action starts with /books. This is WRONG!

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
    );
}

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你曾走过我的故事 2024-12-08 17:58:34

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

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 路由

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"}) }
    );

这可以防止 MVC 接触 URL“/login”和“/contact”

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;
    }
}

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"}) }
    );

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

雨巷深深 2024-12-08 17:58:34

找到了答案。这是一个已知问题。 Glenn Block 发布了一个解决方法。
我刚刚测试过,这确实解决了问题。
它将在下一次下降时修复。

http://codebetter. com/glennblock/2011/08/05/integrating-mvc-routes-and-web-api-routes-2/

Found the answer. It is a known issue. Glenn Block posted a workaround.
I just tested, and this does indeed fix the issue.
It will be fixed with the next drop.

http://codebetter.com/glennblock/2011/08/05/integrating-mvc-routes-and-web-api-routes-2/

时光是把杀猪刀 2024-12-08 17:58:34

在原型分支的下一个分支中,我们有 包含一个解决这个问题的WebApiRoute(使用我提到的方法在我的博客文章中)。 MapServiceRoute 使用它。

In our next drop in our protoype branch we've included a WebApiRoute that addresses this (using the approach I mentioned in my blog post). MapServiceRoute uses it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文