去除URL控制器在一个特定的场景 [英] Removing Controller from url in a specific scenario
问题描述
我想删除控制器
名为首页
从网址
当用户点击关于
和
。我试过,但它给我一个404错误。联系
页> ASP.NET MVC示例应用程序
routes.MapRoute(家,{}动作/(编号),
新{控制器=家});
当我完全删除这一切的作品。
注意我想删除控制器的名称,只有当控制器是家庭。其它控制器应保持不变。
而且我一直在 Route.Config
文件中的code以上。
routes.MapRoute(家,{}动作/(编号),
新{控制器=家}); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
要求的结果
abc.com/Home/About 应显示为 abc.com/About
abc.com/Home/Contact 应显示为 abc.com/Contact
块引用>解决方案您应该在Global.asax(默认的前加入),映射新的路径,例如:
routes.MapRoute(SpecificRouteforHomeController,{}动作/(编号),新{控制器=家,行动=索引,ID = UrlParameter.Optional} );//默认路由任何defalt你想
routes.MapRoute(默认,{控制器} / {行动} / {ID},新{控制器=账户,行动=登录,ID = UrlParameter.Optional});I want to remove
Controller
namedHome
fromurl
when user clicks onAbout
andContact
pages inASP.NET MVC sample application
. I tried this but it is giving me a 404 error.routes.MapRoute("Home", "{action}/{id}", new { controller = "Home" });
When i remove this all works perfectly.
Note I want to remove Controller name only when Controller is Home. Other Controller should remain same. Further I kept the code inRoute.Config
file aboveroutes.MapRoute("Home", "{action}/{id}", new { controller = "Home" }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Required Result
abc.com/Home/About should shown as abc.com/About
abc.com/Home/Contact should shown as abc.com/Contact
解决方案You should map new route in the global.asax (add it before the default one), for example:
routes.MapRoute("SpecificRouteforHomeController", "{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional}); // default route any defalt you want routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Account", action = "Login", id = UrlParameter.Optional} );
这篇关于去除URL控制器在一个特定的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!