Html.ActionLink构造当添加非MVC路线错误的链接 [英] Html.ActionLink construct wrong link when a non-mvc route is added

查看:107
本文介绍了Html.ActionLink构造当添加非MVC路线错误的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里用的表单,MVC的混合应用程序。我指定的路径如下

I have an application here with a mix of webform and mvc. I specify the routing as below

        routes.Add("AspxRoute", new Route("Upload/New", new WebFormRouteHandler<Page>("~/Uploads.aspx")));

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

所以,要上传/新建虚拟路径实际上映射到一个aspx页面的网页表单

So that virtual path to "Upload/New" actually maps to an aspx webform page.

但我的问题是,Html.ActionLink(测试,控制器,行动)现在呈现

But my problem is that Html.ActionLink("Test", "Controller", "Action") now renders

/上传/新控制器=控制器和放大器;动作=动作

/Upload/New?Controller=Controller&Action=Action

说完看了看MVC源$ C ​​$ C,我明白,这是因为ActionLink的调用的 RouteCollection.GetVirtualPath(RequestContext的,routeName,mergedRouteValues​​) 的,其中routeName留给空。并以某种方式默认为使用AspxRoute路由构造URL。我想补充说:AspxRoute之前另一条路线,但似乎总是默认为无MVC routehandler之一。

Having looked at the MVC source code, I understand that it is because ActionLink calls to RouteCollection.GetVirtualPath(requestContext, routeName, mergedRouteValues), where routeName is left to null. And somehow this defaults to use the AspxRoute route to construct the url. I tried to added another route before "AspxRoute", but it seems it always defaults to the non-mvc routehandler one.

如何的 RouteCollection.GetVirtualPath 的行为时routeName为空?为什么它为我的行为的情况下这样?

How does RouteCollection.GetVirtualPath behave when routeName is null? And why is it behaving this way for my case?

如何构建一个正确的网址?我是否需要写一个新的HtmlHelper扩展?

How do I construct a correct url? Do I need to write a new Htmlhelper extension?

干杯

推荐答案

这是另一种选择是一个自定义的约束添加到您的WebFormRoute(S)。例如,您可以创建IRouteConstraint的实现匹配RouteDirection.IncomingRequest,然后用它来确保路由是由服务器生成的途径(如ActionLink的),但仍通过客户端生成的请求中使用的忽略。是这样的:

An alternative option would be to add a custom constraint to your WebFormRoute(s). For example, you could create an implementation of IRouteConstraint to match RouteDirection.IncomingRequest, then use it to ensure the route is ignored by Server-Generated routes (such as ActionLink) but still used by client-generated requests. Something like:

public class IncomingOnlyRouteConstraint: IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.IncomingRequest)
        {
            return true;
        }
        return false;
    }
}

和则约束添加到您的路线:

And then add the constraint to your route:

routes.Add("AspxRoute", new Route("Upload/New", null, 
                            new RouteValueDictionary() { {"WebFormsConstraint", new IncomingOnlyRouteConstraint()} }, 
                            new WebFormRouteHandler<Page>("~/Uploads.aspx")));

当然,你可以preFER添加自己的约束的风格,这个人是相当的限制实现它的航线上,但它只是你能解决这个问题的一种方法的例子。

Of course you may prefer to add your own style of constraint, this one is quite limiting on the route that implements it, but it's just an example of one way you could resolve the issue.

这篇关于Html.ActionLink构造当添加非MVC路线错误的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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