Can I send optional parameters (empty strings, null int?'s etc) to an action through a GET request in asp.net mvc? (one sentence question!)
To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.
You can then use @DefaultValue if you need it: @GET @Path("/job/{param1}/{param2}") public Response method(@PathParam("param1") String param1, @PathParam("param2") String param2, @QueryParam("optional1") String optional1, @QueryParam("optional2") @DefaultValue("default") String optional2) { ... }
Named optional parameters You can call getHttpUrl with or without the third parameter. You must use the parameter name when calling the function. You can specify multiple named parameters for a function: getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) { // ... }
You can do optional parameters with the routing table fairly easily, just specify the defaults in the route of the global.cs file.
So for a search page with an optional query and page you would have something like
RouteTable.Routes.Add(new Route
{
Url = "Search/[query]/[page]",
Defaults = new { controller="Search", action="Results", page=1 },
RouteHandler = typeof(MvcRouteHandler)
});
Default page for your search is then 1.
This example is found here on Scott Gu's blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With