Question

Using APS.NET MVC 4 WebApi

Each request must have a Header called "version" if it doesn't or the version is too low, I need to return an UnAuthorized value to the client. I'm not sure how to send that as a response .

In my BaseApiController I have this:

public abstract class BaseApiController : ApiController
{

    public override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
    {
        var versionText = controllerContext.Request.Headers.GetValues("version").FirstOrDefault();
        if(!string.IsNullOrEmpty(versionText))
           int.TryParse(versionText, out _version);


        ***HERE*** If _version <1 then return UnAuthorized ???? ****  

        return base.ExecuteAsync(controllerContext, cancellationToken);
    }
}
Was it helpful?

Solution

I'm not sure if that's an acceptable way of doing it but you can try out returning a task that will just give you the response you want:

// if version < 1

return new Task<HttpResponseMessage>(() => 
       return Request.CreateResponse(HttpStatusCode.Unauthorized));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top