我需要从项目的URL中删除控制器“Home”,现在它看起来像这样:example.com/Home/contact,我希望它看起来像这样:example.com/contact(也用于其他部分作为服务) ,我们的团队,画廊等)
我尝试删除RouteConfig.cs代码中“{action} / {id}”之前出现的“{controller}”,如下所示:
namespace myproject { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", "{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } }并且控制器“Home”从URL中消失,但以下指向登录和管理的链接不起作用:
@Html.ActionLink("Login here", "Login", "Account") @Html.ActionLink("Manage website", "Index", "Manage")如何在不在URL中显示控制器“Home”的情况下使这些链接工作?
I need to remove the controller 'Home' from the URL of my project, now it looks like this: example.com/Home/contact and i want it to look like this: example.com/contact (also for other sections as services, our-team, gallery, etc.)
I tried to remove the '{controller}' that appears before '{action}/{id}' in RouteConfig.cs code, like this:
namespace myproject { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", "{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } }And the controller 'Home' disappears from the URL, but the following links to login and Manage do not work:
@Html.ActionLink("Login here", "Login", "Account") @Html.ActionLink("Manage website", "Index", "Manage")How could i make those links work without displaying the controller 'Home' in the URL?
最满意答案
您需要将以下内容添加到RouteConfig.cs中
routes.MapRoute( name: "contact", url: "contact", defaults: new { controller = "Home", action = "Contact" } );确保“默认”路由映射是最后一个。
You'll need to add the following to your RouteConfig.cs
routes.MapRoute( name: "contact", url: "contact", defaults: new { controller = "Home", action = "Contact" } );Make sure that the "Default" route mapping is the last one.
更多推荐
发布评论