MVC路由与一个固定的动作和控制器与多个可选参数 [英] MVC routing with one fixed action and controllers with multiple optional parameters
问题描述
基本上我有一个问题,
我想在多个控制器一个默认的动作,并使用多个可选参数,采用如下个性网址:
www.mydomain.com/ {控制器名称} / {V1} / {V2} / {V3} / {} V4
块引用>和也不想在URL操作名称。我有这样的路由在routeconfig.cs
routes.MapRoute(
名称:博客,
网址:{控制器} / {V1} / {V2} / {V3} / {} V4,
默认:新
{
控制器=博客,
行动=searchBlog
V1 = UrlParameter.Optional,
V2 = UrlParameter.Optional,
V3 = UrlParameter.Optional,
V4 = UrlParameter.Optional
});routes.MapRoute(
名称:论坛上,
网址:{控制器} / {V1} / {V2} / {V3} / {} V4,
默认:新
{
控制器=论坛上,
行动=searchForum
V1 = UrlParameter.Optional,
V2 = UrlParameter.Optional,
V3 = UrlParameter.Optional,
V4 = UrlParameter.Optional
});在
行动BlogController
公众的ActionResult searchBlog(字符串V1 = NULL,字符串V2 = NULL,字符串V3 = NULL,字符串V4 = NULL)
{
//使用可选参数点击这里
返回查看(「指数」);
}在
行动ForumController
公众的ActionResult searchForum(字符串V1 = NULL,字符串V2 = NULL,字符串V3 = NULL,字符串V4 = NULL)
{
//使用可选参数点击这里
返回查看(「指数」);
}击中0,3和4个参数我的行动,但不能打的时候传球1或2个参数。
例如
www.mydomain.com/ {控制器名称} / {V1} / {} V2
www.mydomain.com/ {控制器名称} / {} V1
块引用>请帮我/指引我,什么是我提到了我的要求,使用路由在MVC正道。我AP preciate您的宝贵时间。先谢谢了。
解决方案您已经通过固定的路线为每个控制器,否则默认路由配置将被要求的那种情景,你要这样设置的路由配置上述和路由将成为这个样子。
www.mydomain.com/blog/ {V1} / {V2} / {V3} / {} V4
块引用>因为我们已经在此配置固定我们的路线这条路线将只对博客的控制器。
routes.MapRoute(
名称:博客,
网址:博客/ {V1} / {V2} / {V3} / {} V4,
默认:新
{
控制器=博客,
行动=searchBlog
V1 = UrlParameter.Optional,
V2 = UrlParameter.Optional,
V3 = UrlParameter.Optional,
V4 = UrlParameter.Optional
});您必须手动执行此操作为每个控制器的论坛,以及将所得的路由只为论坛控制器工作。
www.mydomain.com/forum/ {V1} / {V2} / {V3} / {} V4
块引用>routes.MapRoute(
名称:论坛上,
网址:论坛/ {V1} / {V2} / {V3} / {} V4,
默认:新
{
控制器=论坛上,
行动=searchForum
V1 = UrlParameter.Optional,
V2 = UrlParameter.Optional,
V3 = UrlParameter.Optional,
V4 = UrlParameter.Optional
});basically i have a problem, i want to make one default action in multiple controllers and use multiple optional parameters with my custom url as below:
www.mydomain.com/{controller name}/{v1}/{v2}/{v3}/{v4}
and also do not want action name in url. I have this routing in routeconfig.cs
routes.MapRoute( name: "Blog", url: "{controller}/{v1}/{v2}/{v3}/{v4}", defaults: new { controller = "Blog", action = "searchBlog", v1 = UrlParameter.Optional, v2 = UrlParameter.Optional, v3 = UrlParameter.Optional, v4 = UrlParameter.Optional }); routes.MapRoute( name: "Forum", url: "{controller}/{v1}/{v2}/{v3}/{v4}", defaults: new { controller = "Forum", action = "searchForum", v1 = UrlParameter.Optional, v2 = UrlParameter.Optional, v3 = UrlParameter.Optional, v4 = UrlParameter.Optional });
action in
BlogController
public ActionResult searchBlog(string v1=null,string v2 = null, string v3 = null, string v4 = null) { // use optional parameters here return View("Index"); }
action in
ForumController
public ActionResult searchForum(string v1=null,string v2 = null, string v3 = null, string v4 = null) { // use optional parameters here return View("Index"); }
my actions hit with 0, 3 and 4 parameters, but can not hit when pass 1 or 2 parameters.
e.g
www.mydomain.com/{controller name}/{v1}/{v2}
www.mydomain.com/{controller name}/{v1}
please help me / guide me, what is right way to use routing in MVC as i mentioned my requirements. i appreciate your valuable time. thanks in advance.
解决方案You have to set the route configuration like this by fixing your route for each of your controllers otherwise the default route configuration will be called for that kind of scenario as you mentioned above and the route will become like this.
www.mydomain.com/blog/{v1}/{v2}/{v3}/{v4}
This route will work only for blog controller as we have fixed our route in this configuration.
routes.MapRoute( name: "Blog", url: "blog/{v1}/{v2}/{v3}/{v4}", defaults: new { controller = "Blog", action = "searchBlog", v1 = UrlParameter.Optional, v2 = UrlParameter.Optional, v3 = UrlParameter.Optional, v4 = UrlParameter.Optional });
You have to manually do this for each of your controllers for the Forum as well and the resultant route will only work for forum controller.
www.mydomain.com/forum/{v1}/{v2}/{v3}/{v4}
routes.MapRoute( name: "Forum", url: "forum/{v1}/{v2}/{v3}/{v4}", defaults: new { controller = "Forum", action = "searchForum", v1 = UrlParameter.Optional, v2 = UrlParameter.Optional, v3 = UrlParameter.Optional, v4 = UrlParameter.Optional });
这篇关于MVC路由与一个固定的动作和控制器与多个可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!