去除URL控制器在一个特定的场景 [英] Removing Controller from url in a specific scenario

查看:95
本文介绍了去除URL控制器在一个特定的场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除控制器名为首页网​​址当用户点击关于联系页> ASP.NET MVC示例应用程序。我试过,但它给我一个404错误。

  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 named Home from url when user clicks on About and Contact pages in ASP.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 in Route.Config file above

 routes.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屋!

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