如何避免"/Home"在网址中 [英] How to avoid "/Home" in the url
问题描述
我正在VS 2013中使用标准MVC模板.
I am using the standard MVC template in VS 2013.
通过默认设置, http://website/将被路由到网站/Home/Index.
With the default set up, http://website/ will be routed to website/Home/Index.
如何直接在网站根网址下路由所有操作",例如 http://website/xxx 到显示与 http://website/Home/xxx 相同的内容?例如,如何使 http://website/About 在Home控制器中执行About操作?如果可能的话,解决方案不应该是Http重定向到 http://website/Home/About ,因为我不知道不想在网址中显示丑陋"的Home/.
How do I route all "actions" directly under website root url, eg http://website/xxx, to show the same content as http://website/Home/xxx? For example, how do I make http://website/About to execute the About action in the Home controller? If possible, the solution shouldn't be a Http redirect to http://website/Home/About because I don't want to show the "ugly" Home/ in the url.
推荐答案
您可以尝试以下方法
routes.MapRoute(
name: "MyAppHome",
url: "{action}/{wa}",
defaults: new { controller = "Home", action = "Index", wa = UrlParameter.Optional, area = "Admin" },
namespaces: new string[] { "MyApp.Controllers" }
).DataTokens = new RouteValueDictionary(new { area = "Admin" });
在这里,您可能会注意到Home控制器是硬编码的,不再在请求中提供.您还可以使用RouteDebugger
来玩路线.
Here, you may notice that the Home controller is hardcoded and is no longer to be supplied in the request. you can also make use of the RouteDebugger
to play with routes.
HTH
这篇关于如何避免"/Home"在网址中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!