ASP.NET MVC 路由在控制器之前带有值并且没有尾部斜杠 [英] ASP.NET MVC Route with values before the controller and no trailing slash

查看:18
本文介绍了ASP.NET MVC 路由在控制器之前带有值并且没有尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但我无法让它发挥作用.

我在 RouteConfig 中指定了这条路线

routes.MapRoute(name: "DefaultSiteRoute",url: "{accountid}/{hostname}/{controller}/{action}/{id}",默认值:new { controller = "Home", action = "Index", id = UrlParameter.Optional, accountid = UrlParameter.Optional, hostname = UrlParameter.Optional });

它适用于这样的网址

<块引用>

/123456/www.test.com/

或者这个

<块引用>

/123456/www.test.com/Controller/Action

但它无法应付这个

<块引用>

/123456/www.test.com

我得到一个 IIS 404

奇怪的是,如果我使用默认的控制器和操作(即主页/索引)为该路由调用 Url.Action ,它会创建一个没有尾部斜杠的 url,然后它不会认出.我真的需要它在有和没有尾部斜杠的情况下工作.

解决方案

问题是 ASP.net 4.0 不路由以 MVC 扩展结尾的 URL.他们这样做是为了加快对静态文件的请求.查看此链接

你可以做什么:

1) 配置 UrlRoutingModule 以路由所有托管和非托管请求(默认仅路由托管请求).

缺点:可能对性能不利.

<块引用>

<模块><删除名称="UrlRoutingModule-4.0"/><add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="runtimeVersionv4.0"/></模块><system.webServer>

2) 配置处理 .com、.net.组织等扩展

缺点:感觉像黑客.

<块引用>

 <处理程序><add name="UrlRoutingHandler"type="System.Web.Routing.UrlRoutingHandler,System.Web,版本=4.0.0.0,文化=中立,PublicKeyToken=b03f5f7f11d50a3a"路径="*.com"动词="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"/>

This is probably a simple question but I just can't get it to work.

I've got this route specified in my RouteConfig

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

And it works fine for a url like this

/123456/www.test.com/

or this

/123456/www.test.com/Controller/Action

but it can't cope with this

/123456/www.test.com

I get an IIS 404

What is stranger is if I call Url.Action for that route with the default Controller and Action (ie Home/Index) it creates a url without a trailing slash, which it then doesn't recognise. I really need it to work with and without the trailing slash.

解决方案

Problem is ASP.net 4.0 doesn't route URLs that ends with an extension to MVC. They do this in order to speed up requests to static files. See this link

What you can do:

1) Configure UrlRoutingModule to route all managed and un-managed requests (default is only route managed requests).

Drawback: May be bad for performance.

<system.webServer>
    <modules>
     <remove name ="UrlRoutingModule-4.0"/>
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="runtimeVersionv4.0" />
    </modules>
<system.webServer>

2) Configure to handle .com, .net. org etc extensions

Drawback: Feels like a hack.

   <system.webServer>
       <handlers>
          <add name="UrlRoutingHandler"
           type="System.Web.Routing.UrlRoutingHandler, 
                 System.Web, Version=4.0.0.0, 
                 Culture=neutral, 
                 PublicKeyToken=b03f5f7f11d50a3a"
                 path="*.com"
                 verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"/>

这篇关于ASP.NET MVC 路由在控制器之前带有值并且没有尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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