I have your typical API Route, however I need it to still return with the Ok JSON blob that it is using, however I need to specify the HttpStatusCode as something different.
Here is the API method.
[Route("detailedvarinfo/{VarGUID}")]
public async Task<IHttpActionResult> GetDetailedVarInfo(string VarGUID)
{
if (!User.IsInRole("Admin"))
{
var DashboardAccess = (from DR in AuthDb.DashboardAccessVars
where DR.ApplicationUser.Id == userInfo.Id
select DR).
AsEnumerable()
.Select(x => new
{
VarGUID = x.VarGUID
}).ToList();
var FilteredVarInfo = VarInfo.Join(DashboardAccess, x => x.VarGUID, y => y.VarGUID, (x, y) => x);
if (FilteredVarInfo.Any())
{
return Ok(FilteredVarInfo.FirstOrDefault());
}
else
{
return Ok(HttpStatusCode.Forbidden);
}
}
}
I looked at implementing this but did not have any luck. Return content with IHttpActionResult for non-OK response
Also tried to do something like this.. but did not have any luck.
public class NotAllowedOkResult<T> : OkNegotiatedContentResult<T>
{
public NotAllowedOkResult(T content, ApiController controller, HttpStatusCode statusCode)
: base(content, controller)
{
}
public NotAllowedOkResult(T content, IContentNegotiator contentNegotiator, HttpRequestMessage request,
IEnumerable<MediaTypeFormatter> formatters, HttpStatusCode statusCode)
: base(content, contentNegotiator, request, formatters)
{
}
public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.ExecuteAsync(cancellationToken);
return response;
}
}