Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 routing optional parameters

I've an Action in my ApiController that I want to invoke from a specific link, so I created this simple route

[Route("Rest/GetName/{name}")]
public IHttpActionResult GetName(string name) {
    // cut - code here is trivial but long, I just fill in an object to return as Json code
    return Json(myObject);
}

It works fine but I want to make the parameter optional. According to documentation adding a question point at the end of the parameter name in the route should be enough

[Route("Rest/GetName/{name?}")]

This way I get an error if I don't provide the optional parameter, so

.../Rest/GetName/AnyName --> ok
.../Rest/GetName/ --> error (see below)

{"Message":"No HTTP resource was found that matches the request URI 'https://localhost/miApp/Rest/GetName'.","MessageDetail":"No action was found on the controller 'Rest' that matches the request."}

like image 790
Naigel Avatar asked Dec 14 '15 10:12

Naigel


People also ask

Can we make URI parameter optional?

You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

How do you pass optional parameters in C#?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

How do you make a parameter optional in a URL?

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?}")]


2 Answers

Web API requires to explicitly set optional values even for nullable types and classes.

Use default value to optional parameter:

[Route("Rest/GetName/{name?}")]
public IHttpActionResult GetName(string name = null) {
    // cut - code here is trivial but long, I just fill in an object to return as Json code
    return Json(myObject);
}

And don't forget about routing registration:

httpConfig.MapHttpAttributeRoutes()
like image 158
Vadim Martynov Avatar answered Sep 16 '22 14:09

Vadim Martynov


There are many possible solutions:

  1. Try Optional Parameter

       [Route("Rest/GetName/{name?}")]
       public IHttpActionResult GetName(string name = null) {
           // cut - code here        is trivial but long, I just fill in an               
           obj ect to return as 
           `enter code here`Json code
           return Json(myObject);
             }
    

2.Set PreFix on controller first

    [RoutePrefix("api/Rest")]
    [Authorize]
    public class RestController : ApiController
    {
         [Route("/GetName/{name}")]
         public IHttpActionResult GetName(string name = null) 
         {
         // cut - code here is trivial but long, I just fill in an object
         to  return as Json code
         return Json(myObject);
         }
     }

3.Write parameter before action name in route

   [RoutePrefix("api/Rest")]
    [Authorize]
    public class RestController : ApiController
    {
         [Route("{name}/GetName")]
         public IHttpActionResult GetName(string name = null) 
         {
         // cut - code here is trivial but long, I just fill in an object
         to  return as Json code
         return Json(myObject);
         }
     }

Hopefully this will help you to resolve your problem.Thanks

like image 24
Nauman Khan Avatar answered Sep 20 '22 14:09

Nauman Khan