13

When the scaffold for a .NET core Web API is used it includes:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

in the ConfigureServices method and

app.UseMvc();

in the Configure method.

Why is this? A web API doesn't need "Model View Controller". It might only need "Model" and "Controller". I relate MVC very much to Views and Razor. Can these declarations be left out?

4
  • 3
    The migration guide covers it: ASP.NET Core unifies ASP.NET 4.x's MVC and Web API app models into a simpler programming model known as ASP.NET Core MVC.. Jul 23, 2019 at 9:03
  • 1
    Simply, there is no ASP.NET Core MVC and ASP.NET Core Web Api. It's just ASP.NET Core. Adding "MVC" or "Web Api" is a way of describing a style, but the underlying framework is the same for both. There is no true difference. You can have MVC-style actions in an "API" controller, and API-style actions in an "MVC" controller. Jul 23, 2019 at 12:59
  • 2
    @ChrisPratt coming from ASP.NET MVC it is confusing. I just learned that a pure API controller derives from ControllerBase where a MVC controller derives from Controller which then provides View-related functionality. Jul 23, 2019 at 14:12
  • That's actually new since 2.2. Traditionally, you'd just inherit from Controller there too. There's really no difference either way, it's just that ControllerBase doesn't expose stuff that's meaningless for an API, so it makes intellisense cleaner. Jul 23, 2019 at 15:27

2 Answers 2

7

If we check the source code for the internal AddMvc extension we can see these things clearly:

public static IMvcBuilder AddMvc(this IServiceCollection services)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }

        var builder = services.AddMvcCore();

        builder.AddApiExplorer();
        builder.AddAuthorization();

        AddDefaultFrameworkParts(builder.PartManager);

        // Order added affects options setup order

        // Default framework order
        builder.AddFormatterMappings();
        builder.AddViews();
        builder.AddRazorViewEngine();
        builder.AddRazorPages();
        builder.AddCacheTagHelper();

        // +1 order
        builder.AddDataAnnotations(); // +1 order

        // +10 order
        builder.AddJsonFormatters();

        builder.AddCors();

        return new MvcBuilder(builder.Services, builder.PartManager);
    }

In my opinion, for Web API, you probably need AddJSONFormatters() and AddCors(). However, you would also need AddMvcCore(). This includes stuff like routing, attributes, filters, result executors, model binders, controllers etc.

Refer to https://codingblast.com/using-web-api-asp-net-core-without-mvc-specific-stuff/

4

app.UseMvc() tells your app to add MVC to the request execution pipeline. This will ensure that all requests to your web application are routable to the MVC framework, meaning you can use controllers, views and anything else contained within the MVC implementation (action filters etc).

if you don't need view functionality then you

Don't create a web API controller by deriving from the Controller class. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. There's an exception to this rule: if you plan to use the same controller for both views and APIs, derive it from Controller. The ControllerBase class provides many properties and methods that are useful for handling HTTP requests.

For more details checkout this link. https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.