9

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."}

asked Dec 14, 2015 at 10:56

2 Answers 2

19

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()
answered Dec 14, 2015 at 11:01
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Dec 14, 2015 at 12:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.