Difference between ApiController and IHttpController inheritance

While I have been using Microsoft.NET Framework for several years, I start when it comes to ASP.NET, in particular ASP.NET WebAPI Framework. I am considering using the ASP.NET WebAPI framework for a small project.

I looked at the ASP.NET WebAPI poster and noticed that the class Controllercan:

  • Withdraw from class ApiController or
  • Implement interface IHttpController

Reading the MSDN documentation for the class ApiControllershows what it implements IHttpController. It seems like it just declares one method called . IHttpControllerExecuteAsync()

What I don’t understand: under what circumstances should one derive from ApiControlleror simply implement an IHttpControllerinterface method ExecuteAsync()? What are the pros and cons of each approach?

+4
source share
2 answers

If this is the same as the base controllers in MVC: the easiest way is to get only the base class ( ApiController). This provides an extra layer of abstraction between you and some basic request processing that you usually want to avoid by yourself.

In the case of ASP.NET MVC, you can create your own class IControllerif you want to customize how requests are processed. This includes action methods, image rendering, etc.

Controller ( ApiController) ASP.NET .

IHttpController, "" ; ApiController, -.

A Controller ExecuteAsync() (Execute MVC) . requestContext; .

, , .

+4

IHttpController, . HTTPRequestMessage HTTPResponseMessage.

IHttpController , . HttpContent, ApiController.

public class SimpleController : IHttpController
{
    public async Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
    {
        return new HttpResponseMessage(HttpStatusCode.OK)
        {
            RequestMessage = controllerContext.Request,
            Content = new StringContent("Hello World")
        };
    }
}
+2

Source: https://habr.com/ru/post/1524572/


All Articles