如何删除 /Home/ 但在多个控制器时将其保留为默认值
How to remove /Home/ but keep it as default when multiple controllers
我看过一些类似的帖子,但是 none 完全一样,因为在我的问题中,我想从 URL 中删除 HOME 控制器,但控制器保持默认(因此,当我的网站从 site.com/ 加载时,我的 Home 控制器中的索引被触发)!!
我的MVC4网站有几个控制器,比如
首页
产品
详情
还有一个
还有一个
主页控制器是我的默认控制器,是我的主页、关于页面、联系我们等所在的地方
我想从 URL 中删除 HOME 但这仍然是我的默认控制器。所有其他控制器应保留控制器名称。
下面不行,请注意默认控制器"WhatControllerGoesHere"
routes.MapRoute(
name: "HomeController",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "WhatControllerGoesHere", action = "Index", id = UrlParameter.Optional }
);
如果我将两个默认控制器设为 HOME,则无法导航到任何其他控制器
routes.MapRoute(
name: "HomeController",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
所以,我想要实现的 URL 是(关于和联系我们的地方是我主页中的视图)
site.com/ //note, no Home/
site.com/about //note, no Home/
site.com/contact //note, no Home/
site.com/products/
site.com/products/other
site.com/details/
site.com/details/other
site.com/details/AndAnother
我知道我可以为每个控制器创建新的路由器,但我宁愿尽可能避免这样做。
除 site.com/about
和 site.com/contact
外,您显示的所有 url 都将由默认路由正确处理。要从 about
和 contact
url 中删除 Home/
前缀,您需要在默认值之前添加 2 个特定路由(假设您不需要 id
参数)
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我看过一些类似的帖子,但是 none 完全一样,因为在我的问题中,我想从 URL 中删除 HOME 控制器,但控制器保持默认(因此,当我的网站从 site.com/ 加载时,我的 Home 控制器中的索引被触发)!!
我的MVC4网站有几个控制器,比如
首页
产品
详情
还有一个
还有一个
主页控制器是我的默认控制器,是我的主页、关于页面、联系我们等所在的地方
我想从 URL 中删除 HOME 但这仍然是我的默认控制器。所有其他控制器应保留控制器名称。
下面不行,请注意默认控制器"WhatControllerGoesHere"
routes.MapRoute(
name: "HomeController",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "WhatControllerGoesHere", action = "Index", id = UrlParameter.Optional }
);
如果我将两个默认控制器设为 HOME,则无法导航到任何其他控制器
routes.MapRoute(
name: "HomeController",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
所以,我想要实现的 URL 是(关于和联系我们的地方是我主页中的视图)
site.com/ //note, no Home/
site.com/about //note, no Home/
site.com/contact //note, no Home/
site.com/products/
site.com/products/other
site.com/details/
site.com/details/other
site.com/details/AndAnother
我知道我可以为每个控制器创建新的路由器,但我宁愿尽可能避免这样做。
除 site.com/about
和 site.com/contact
外,您显示的所有 url 都将由默认路由正确处理。要从 about
和 contact
url 中删除 Home/
前缀,您需要在默认值之前添加 2 个特定路由(假设您不需要 id
参数)
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);