Skip to content

Instantly share code, notes, and snippets.

@sandrock
Created November 6, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandrock/726cb5c1862688ff2127e13120adac92 to your computer and use it in GitHub Desktop.
Save sandrock/726cb5c1862688ff2127e13120adac92 to your computer and use it in GitHub Desktop.
ASP WebAPi better ApiController NotFound method with negociated response content
namespace MyWebApi
{
public abstract class ApiController : System.Web.Http.ApiController
{
/// <summary>
/// Returns a NotFound HTTP result (404) with a negotiated full-bodyed result.
/// </summary>
/// <returns></returns>
protected override System.Web.Http.Results.NotFoundResult NotFound()
{
var result = new MyWebApi.NotFoundResult(this); // this inherits from System.Web.Http.Results.NotFoundResult
var error = new HttpError("Not found");
error.Add("HttpCode", 404);
error.Add("ErrorCode", "NotFound");
error.Add("ErrorDetail", "No such entity. ");
error.Add("ErrorMessage", "No such entity. ");
error.Add("Succeed", false);
result.Error = error;
return result;
}
/// <summary>
/// Returns a NotFound HTTP result (404) with a negotiated full-bodyed result.
/// </summary>
/// <returns></returns>
protected System.Web.Http.Results.NotFoundResult NotFound(string message)
{
var result = new MyWebApi.NotFoundResult(this); // this inherits from System.Web.Http.Results.NotFoundResult
var error = new HttpError("Not found");
error.Add("HttpCode", 404);
error.Add("ErrorCode", "NotFound");
error.Add("ErrorDetail", message ?? "No such entity. ");
error.Add("ErrorMessage", message ?? "No such entity. ");
error.Add("Succeed", false);
result.Error = error;
return result;
}
/// <summary>
/// Returns a forbidden HTTP result (403) with a negotiated full-bodyed result.
/// </summary>
/// <returns></returns>
protected IHttpActionResult Forbidden()
{
var error = new HttpError("Forbidden");
error.Add("HttpCode", 403);
error.Add("ErrorCode", "Forbidden");
error.Add("ErrorDetail", "Access denied. ");
error.Add("ErrorMessage", "Access denied. ");
error.Add("Succeed", false);
var result = new NegotiatedContentResult<HttpError>(HttpStatusCode.Forbidden, error, this);
return result;
}
}
}
namespace MyWebApi
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
/// <summary>
/// Custom implementation of the NotFoundResult that returns a negociated content.
/// </summary>
public class NotFoundResult : System.Web.Http.Results.NotFoundResult
{
private readonly ApiController controller;
public NotFoundResult(HttpRequestMessage request)
: base(request)
{
}
public NotFoundResult(ApiController ctrl)
: base(ctrl)
{
this.controller = ctrl;
}
public HttpError Error { get; set; }
public override Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var message = this.Request.CreateResponse(HttpStatusCode.NotFound);
if (this.Error == null)
{
this.Error = new HttpError();
this.Error.Add("HttpCode", 404);
this.Error.Add("ErrorCode", "NotFound");
this.Error.Add("ErrorDetail", "No such entity. ");
this.Error.Add("ErrorMessage", "No such entity. ");
this.Error.Add("Succeed", false);
}
var negociator = new NegotiatedContentResult<HttpError>(HttpStatusCode.NotFound, this.Error, this.controller);
return negociator.ExecuteAsync(cancellationToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment