MVC routing with one fixed action and controllers with multiple optional parameters

让人想犯罪 __ 提交于 2019-12-10 20:29:56

问题


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

  1. www.mydomain.com/{controller name}/{v1}/{v2}

  2. 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.


回答1:


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
        });


来源:https://stackoverflow.com/questions/35449481/mvc-routing-with-one-fixed-action-and-controllers-with-multiple-optional-paramet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!