3

我正在尝试将现有的 Web 项目转换为 MVC。我在 VS 2012 中做了一个标准的 MVC 项目。它添加了路由配置。我现有的项目已经包含 WCF 服务使用的路由条目。所以现在路由配置如下:

// Was here before, used by services
routes.Add(new ServiceRoute("AppServer", new WebServiceHostFactory(), typeof(MyService)));

// the rest is added by vs to configure a default controller
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

我意识到服务部分应该始终放在第一位,因为它更具限制性。如果放在最后,服务将不起作用。但现在的问题是

@Html.ActionLink("text", "MyAction", "MyController")

现在生成我类型的链接

http://localhost/AppServer?action=MyAction&controller=My

代替

http://localhost/My/MyAction

我所期望的。

有谁知道如何使服务路由和与 mvc 相关的路由“和平相处”,即不相互影响?

4

1 回答 1

2

尝试将 MapRoute 放在顶部,但为约束添加第四个参数,如下所示:

new { controller = "regex-for-!=-AppServer" }

这样,在创建链接时,助手将使用找到的第一个路由,但仍将跳过传入到“/AppServer”的请求,并由 ServiceRoute 处理。

于 2012-10-23T20:17:50.560 回答