locked
WebAPI: why does it use MVC namespace and classes? RRS feed

  • Question

  • User1568133431 posted

    Hello,

    I'm pretty new to the WebAPI world.

    So far I've read some great articles on how to create and call WebAPI; however, I'm still a bit puzzled about one thing: even if we create "just" a WebAPI, not a MVC application, we are almost "stuck" with the Microsoft.AspNetCore.Mvc namespace.

    For instance:

    • ControllerBase is part of the namespace;
    • HttpMethodAttribute (HttpGet, HttpPost, etc.) is part of the namespace;
    • even return methods like Ok(), CreatedAtRoute(), etc. are part of the namespace.

    Why is this like that? Can't we have a "proper" separation between "pure WebAPI" and MVC application? Is that the correct way of doing thing? If no, what is the correct way of creating a "pure WebAPI"?

    Thanks and have a nice day :).

    Thursday, March 12, 2020 2:00 PM

All replies

  • User-474980206 posted

    in asp.net core web api controllers are the same code as mvc controllers. there is an apicontroller attribute, but its only used by routing to add the api path.  for a pure webapi you drop the view engine support not controller support.

    Thursday, March 12, 2020 2:20 PM
  • User-782232518 posted

    Why is this like that? Can't we have a "proper" separation between "pure WebAPI" and MVC application? Is that the correct way of doing thing? If no, what is the correct way of creating a "pure WebAPI"?

    It is like that because ASP.NET 4.x used to use different controllers for MVC and Web API, but that approach turned out to be a total failure with horrible code duplication.

    When Microsoft designed ASP.NET Core from scratch (named ASP.NET 5 initially), they learned from those mistakes, https://www.dotnetcurry.com/aspnet/1061/aspnet5-mvc-webapi-entityframework-application

    Friday, March 13, 2020 2:05 AM
  • User1120430333 posted

    Hello,

    I'm pretty new to the WebAPI world.

    So far I've read some great articles on how to create and call WebAPI; however, I'm still a bit puzzled about one thing: even if we create "just" a WebAPI, not a MVC application, we are almost "stuck" with the Microsoft.AspNetCore.Mvc namespace.

    For instance:

    • ControllerBase is part of the namespace;
    • HttpMethodAttribute (HttpGet, HttpPost, etc.) is part of the namespace;
    • even return methods like Ok(), CreatedAtRoute(), etc. are part of the namespace.

    Why is this like that? Can't we have a "proper" separation between "pure WebAPI" and MVC application? Is that the correct way of doing thing? If no, what is the correct way of creating a "pure WebAPI"?

    Thanks and have a nice day :).

    ASP.NET MVC WebAPI project structure is the same as the ASP.NET MVC project structure, except that the WebAPI project doesn't have a view folder. You can add a MVC UI controller and view folder in the WebAPI project and server up pages in the WebAPI project, becuase they are both ASP.NET MVC.

    This tutorial is  doing that of severing up views in a ASP.NET MVC WebAPI project.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-1

    https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_web_api.htm

     

    Friday, March 13, 2020 6:08 AM
  • User1568133431 posted

    Thanks a lot for all your answers :)!

    Sunday, March 22, 2020 3:55 PM
  • User475983607 posted

    MVC Actions and Web API Actions are the same in Core.  

    Why is this like that? Can't we have a "proper" separation between "pure WebAPI" and MVC application? Is that the correct way of doing thing? If no, what is the correct way of creating a "pure WebAPI"?

    Configuration determines what returned in ASP.NET Core 3.1.  AddControllers() does not render View or Pages

    services.AddControllers();

    There's AddControllersWithViews() which registers Views and AddRazorPages() for Razor Pages.  The standard docs explain the details and the following blog explains the configuration; https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/.

    Sunday, March 22, 2020 7:05 PM