如何避免"/Home"在网址中 [英] How to avoid "/Home" in the url

查看:219
本文介绍了如何避免"/Home"在网址中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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屋!

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