3

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 Answer 1

5

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
        });
Sign up to request clarification or add additional context in comments.

2 Comments

You cannot have multiple optional parameters. Only the last parameter can be UrlParameter.Optional (the routing engine has no way of matching up values if only 1 or 2 or 3 parameters are provided so they are converted to query string values, not route values. Remove UrlParameter.Optional from all but the last parameter.
@Stephen yes, its the point, but my need is only query string values overall the url. thanks for your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.