如何网址隐藏控制器的名字吗? [英] How to hide controller name in Url?
问题描述
如何网址隐藏控制器名称?
How to hide controller name in Url?
我用的是ASP.NET MVC。
I use the ASP.NET MVC.
原始URL是: http://www.sample.com/Users.mvc / UserDetail / 9615
用户是控制器的名称,将UserDetail是行动的名字,而9615是用户ID。
The "Users" is controller name, the "UserDetail" is action name, and the "9615" is UserId.
如何隐藏在URL中的控制器名称和动作名称。
How can I hide the controller name and action name in the url.
就像这样: http://www.sample.com/9615
我所著的Global.ascx.cs以下code隐藏动作名称:
I have writed the following code in the Global.ascx.cs to hide the action name:
routes.MapRoute(
"UserDetail", // Route name
"Users.mvc/{UserId}", // URL with parameters
new { controller = "Users", action = "UserDetail", UserId = "" } // Parameter defaults
);
使用上述code我躲在动作的名称,并得到了这个网址:的http:/ /www.sample.com/Users.mvc/9615
但我怎么能隐藏控制器名称,并得到这个网址: http://www.sample.com/9615
But how can I hide the controller name and get this url: http://www.sample.com/9615
感谢。
推荐答案
的想法是一样的。你只做你做动作的东西。然而,你的问题出现的原因是,IIS可能是不映射到www.xyz.com/1234 ASP.NET运行时。在IIS7为此,请启用集成模式,并在IIS6,加入处理地图通配符映射映射一切ASP.NET。
The idea is the same. You do just the thing you did to the action. However, your problem arises from the fact that IIS is probably not mapping www.xyz.com/1234 to ASP.NET runtime. To do so in IIS7, enable integrated mode and in IIS6, add a wildcard mapping in handler map that maps everything to ASP.NET.
要添加通配符映射,看到的 http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx (搜索,在IIS6扩展名的网址页)
To add a wildcard map, see http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx (Search for "IIS6 Extension-less URLs" in that page)
在此之后,只需添加一个路线:
After that, simply add a route:
routes.MapRoute("UserDetails", "{UserID}/{*name}",
new { controller = "Users", action = "UserDetail" , UserID=""});
这应该做的伎俩。
这篇关于如何网址隐藏控制器的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!