在ASP.NET MVC 4路线删除尾部斜杠应用程序根目录 [英] Remove Trailing Slash in ASP.NET MVC 4 Route to Application Root
问题描述
在我的ASP.NET MVC 4应用程序的 RouteConfig
文件,我已经注册了以下默认路由:
routes.MapRoute(默认,{控制器} / {行动} / {ID}
新{控制器=家,行动=指数,ID = UrlParameter.Optional});
现在,在我的剃须刀的意见,我想生成一个URL到我的应用程序的根这样的:
< A HREF =@ Url.Action(指数,家)>家庭和LT; / A>
生成的URL包括结尾的斜线;点击链接从而打开页面的本地主机/ IISApplicationName 的/。但是,我想要的网址不包含斜线,这样该URL的本地主机/ IISApplicationName 的。其他操作产生途径,如 /帐号/登录的,不会产生尾随斜杠的URL - 它只是路线链接到我的应用程序的根目录
有什么办法prevent ASP.NET MVC路由从追加结尾的斜线上述路线?
(我知道我可以从URL包括斜线到一个没有重定向,但我宁愿路由产生在首位的正确路径URL)。
有什么办法prevent ASP.NET MVC路由从追加
斜线上述路线?
块引用>您应该当相对路径为空传递一个斜线。如果你以某种方式生成HREF链接
本地主机/ IISApplicationName
仍是浏览器附加一个/结尾(你可以很容易在Firebug验证这一点,只需在浏览器地址栏和检查中键入 http://stackoverflow.com 请求选项卡可以看到结尾的/\").这是因为每HTTP / 1.1的,
的绝对路径不能emtpy;如果没有为present原
URI,它MUT被指定为/(服务器根目录)。
块引用>http://www.faqs.org/rfcs/rfc2616.html#ixzz0kGcaRbqU (5.1.2节)
In my ASP.NET MVC 4 application's
RouteConfig
file, I have registered the following default route:routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = UrlParameter.Optional });
Now, in my Razor views, I want to generate a URL to my application's root like that:
<a href="@Url.Action("index", "home")">Home</a>
The generated URL includes a trailing slash; clicking the link thus opens the page localhost/IISApplicationName/. However, I want the URL not to contain the trailing slash so that the URL is localhost/IISApplicationName. Generating routes for other actions, such as /Account/Login, does not create URLs with trailing slashes — it's just about the route linking to my application's root.
Is there any way to prevent ASP.NET MVC routing from appending the trailing slash to the above route?
(I know that I can redirect from the URL including the slash to the one without it, but I'd rather have routing generate the correct route URL in the first place.)
解决方案Is there any way to prevent ASP.NET MVC routing from appending the trailing slash to the above route?
You should pass a trailing slash when the relative path is empty. If you somehow generate links with href
localhost/IISApplicationName
still the browser appends a "/" at the end (you can easily verify this in firebug, just type http://stackoverflow.com in the browser address bar and check the requests tab you can see the trailing "/").This is because as per the HTTP/1.1,
the absolute path cannot be emtpy; if none is present in the original URI, it mut be given as "/" (the server root).
http://www.faqs.org/rfcs/rfc2616.html#ixzz0kGcaRbqU (section 5.1.2)
这篇关于在ASP.NET MVC 4路线删除尾部斜杠应用程序根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!