使用 WCF ServiceRoute 进行 MVC2 路由:Html.ActionLink 呈现不正确的链接! [英] MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links!
问题描述
我有一个与 MVC2 网站并排存在的 WCF 服务.我希望我的服务 URL 看起来像这样:
I have a WCF service that lives side-by-side with an MVC2 web site. I'd like for my URL for the service to look like this:
http://localhost/projdir/Service
MVC 网站还处于起步阶段,因此它仍然拥有所有样板控制器等.
The MVC site is in its infancy so it still has all its boilerplate controllers etc.
以下代码在 global.asax 中乍一看是有效的:
The following code works at first glance in global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("Service", new ServiceHostFactory(),
typeof(MyService)));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
}
该服务出现在我所描述的位置,并且按照宣传的方式工作.太好了.
The service appears just where I described and works as advertised. Great.
但是,我只是注意到以这种方式对我的代码进行排序会改变我所有的 ActionLink
.例如,MVC 站点上的关于"选项卡现在如下所示:
However, I just noticed that ordering my code this way changes all of my ActionLink
's. For example, the "About" tab on the MVC site now appears like this:
http://localhost/projdir/Service?action=About&controller=Home
这显然是不正确的(应该是 http://localhost/projdir/Home/About/
).
This is obviously incorrect (it should be http://localhost/projdir/Home/About/
).
如果我将 ServiceRoute
添加到默认 MapRoute()
调用下方,则会收到缺少控制器错误.(实际上我得到了一个StructureMapControllerFactory 没有返回控制器的实例"错误,因为我已经连接了 StructureMap,呃,它一开始就不是控制器.)
If I move the ServiceRoute
addition below the default MapRoute()
call, then I get a missing controller error. (Actually I get a "StructureMapControllerFactory did not return an instance of a controller" error, because I'm wired up with StructureMap, duh, it's not a controller to begin with.)
有趣的是,它似乎只影响了 Html.ActionLink()
的输出.我可以手动输入 http://localhost/projdir/Home/About/
并进入正确的页面.
Interestingly, it only seems to be affecting the output of Html.ActionLink()
. I can manually type in http://localhost/projdir/Home/About/
and get to the correct page.
我犯了什么可怕的新手错误?
What horribly obvious newbie mistake am I making?
推荐答案
尝试在 MVC 路由之后移动服务路由.但是要避免之前遇到的缺少控制器"错误,请添加 MVC 路由 with a 路由约束.这些路由约束可以是正则表达式——基本上你希望你的路由约束是任何 不是服务"的控制器.当请求服务"时,它会通过 WCF 服务路由.
Try moving the Service route after the MVC route. But to avoid the "missing controller" error that you got before, add the MVC route with a Route Constraint. These route constraints can be Regex - basically you'd want your route constraint to be any controller that is not "Service". When a request for "Service" is requested, it will make it fall through and his the WCF Service Route.
这篇关于使用 WCF ServiceRoute 进行 MVC2 路由:Html.ActionLink 呈现不正确的链接!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!