MVC2路由与WCF ServiceRoute:Html.ActionLink渲染不正确的链接! [英] MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links!

查看:123
本文介绍了MVC2路由与WCF ServiceRoute:Html.ActionLink渲染不正确的链接!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有生活并排侧与MVC2网站的WCF服务。我想为我的网址为服务看起来是这样的:

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://本地主机/ PROJDIR /服务

该MVC网站还处于起步阶段,因此仍然有它的所有样板控制器等。

The MVC site is in its infancy so it still has all its boilerplate controllers etc.

以下code工作乍一看在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.

不过,我只注意到我的订货code这种方式改变了我所有的 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://本地主机/ PROJDIR /服务行动=关于&安培;控制器=首页

这是显然是不正确(应该是的http://本地主机/ PROJDIR /首页/ / )。

This is obviously incorrect (it should be http://localhost/projdir/Home/About/ ).

如果我移动 ServiceRoute 除了默认低于图路线()电话,然后我得到一个失踪控制器错误。 (其实我得到一个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://本地主机/ PROJDIR /首页/ / ,并获得了正确的页面。

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路线的的一个的路由约束。这些路由约束可以是正则表达式 - 基本上你会希望你的路由约束是任何控制器,它的的服务。当要求对服务的请求时,它将使告吹,他的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.

这篇关于MVC2路由与WCF ServiceRoute:Html.ActionLink渲染不正确的链接!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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