Hristo Hristo - 1 year ago 147
C# Question

How to return NotFound result when using web api documentation

How to indicate that a resource cannot be found and still get auto generated documentation for my Web API 2 web application?

Take for example this method definiition:

public JsonResult<CalculatorDescription> GetParameterInfo(string parameterCaption)

If I use this definition I cannot return
, yet if I change the return type to
( in order to be able to return
) I do not get the auto-generated information for my return type.

Note that I'm using the JsonResult and not the MVC one ( )

Amy Amy
Answer Source

Change your return type to IHttpActionResult, and decorate your action method with the ResponseType attribute:

Use this to specify the entity type returned by an action when the declared return type is HttpResponseMessage or IHttpActionResult. The ResponseType will be read by ApiExplorer when generating ApiDescription.

Then, inside your method, return NotFound() or JsonResult<T>.

public IHttpActionResult GetParameterInfo(string parameterCaption)
    if (...) 
        return new JsonResult<CalculatorDescription>(...);
        return NotFound();        
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download