view raw
Hristo Hristo - 6 months ago 38
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

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();