Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp mvc routing with two optional parameters

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?

Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id

        routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
like image 958
0wen101 Avatar asked Feb 21 '11 00:02

0wen101


People also ask

How to make route parameter optional in MVC?

You can make a URI parameter optional by adding a question mark to the route parameter. If a route parameter is optional, you must define a default value for the method parameter.

Can we have multiple optional parameters in C#?

C# Multiple Optional ParametersIf you want to define multiple optional parameters, then the method declaration must be as shown below. The defined GetDetails() method can be accessed as shown below. GetDetails(1); GetDetails(1, "Rohini");

Can we have multiple routing in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

How to make route parameter optional in. net Core?

We can make a route parameter optional by adding “?” to it. The only gimmick is while declaring a route parameter as optional, we must specify a consequent default value for it: [HttpGet("GetById/{id?}")] In this case, we assign 1 as a default value for the id parameter in the GetById action method.


2 Answers

You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.

I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.

Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.

// used when both parameters are specified
routes.MapRoute(
        "Company+Profession", // Route name
        "{action}/{value}/{profId}", // URL with parameters
        new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);
like image 157
tvanfosson Avatar answered Sep 23 '22 12:09

tvanfosson


Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :

        routes.MapRoute(
           "Detail", // Route name
           "{action}/{value}", // URL with parameters
           new { controller = "Companies", action = "Detail" }, // Parameter defaults
           new { value = @"\d+" } //integer only
        );

        routes.MapRoute(
            "Company + Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Results" }, // Parameter defaults
            new { profId = @"\d+" } //integer only
        );

        routes.MapRoute(
            "Profession", // Route name
            "{action}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Results"}, // Parameter defaults
            new {profId = @"\d+" } //integer only
        );

        routes.MapRoute(
           "Company", // Route name
           "{action}/{value}", // URL with parameters
           new { controller = "Companies", action = "Results" } // Parameter defaults
       );

        routes.MapRoute(
            "RootFolder", // Route name
            "{action}/{value}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
        );
like image 40
0wen101 Avatar answered Sep 21 '22 12:09

0wen101