In this article we are going to discuss Difference between AddMVC() and AddMvcCore(). Please read our previous article how to setup MVC in ASP .NET Core application.

Along with the AddMvc() method, we can also use the AddMvcCore() method to Set up the MVC in the ASP.NET Core application. So, in short, we can say that the IServiceCollection interface provides two methods such as AddMvc() and AddMvcCore() to set up MVC in an ASP.NET Core application.

Before we discuss the difference between AddMvc() and AddMvcCore() methods, let’s modify the following HomeController to return JSON formatted data, instead of a simple string. 

At the moment, there is no base class specified for the the HomeController class. It works for us now because we are returning a simple string from the Index() action method like below;

public class HomeController : Controller
    {
        public string Index()
        {
            return "Hello from MVC | Coreprogram";
        }
    }

As you can see in the above code, the HomeController class is not inherited from the base Controller class. But still, it works as expected and we get the output when we run the application. This is because within the index action method we are simply returning a string. 

But this is not going to be worked if we want to return an HTML View or JSON Data from the Index action method. If you want to return the JSON Data or HTML View from an action method then the Controller class should inherit from the base Controller class which is provided by the ASP.NET Core framework.

The base Controller class which is belongs to Microsoft.AspNetCore.Mvc namespace provides the supports to return different types of results such as JSONResult, ViewResult, etc.

public class HomeController : Controller
    {
        public JsonResult Index()
        {
            return Json(new { Name = "John Smith", Country = "USA", Designation = "Software Engineer" });
        }
    }

And as like below image we added services.AddMvc(); in the ConfigureServices() method.

CoreSpider - ConfigureServices()

When we run the application you can see we got the Json result as expected.

Json Result

Using AddMvcCore() method

Now let us modify the ConfigureServices() method of the Startup class as shown below to use the AddMvcCore() method to register the MVC services to the dependency injection container.

AddMVCCore()

With the above changes in the ConfigureServices() method, when you run the application you can find the below error.

Internal Server Error
Let’s understand why the above issue is came when we use AddMvcCore()

The reason is that AddMvc() method registered the Json Formatters within the dependency injection container but not the AddMvcCore() method. So that when we run the application it shows the straight forward error.

As asp.net core is open source, so you can find the source code of the AddMvc() method by navigating to the following GitHub page. https://github.com/aspnet/Mvc/blob/edb5baf81c07e3e9db8c3019439414094acb6ef8/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs

So let’s modify the ConfigureServices() method of the Startup class as shown below to register the JsonFormatters with AddMvcCore() method to return JSON data.

public void ConfigureServices(IServiceCollection services)
        {
            var builder = services.AddMvcCore();
            builder.AddJsonFormatters();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();

            app.UseMvcWithDefaultRoute();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

Now, when we run the application we can see the Json result as expected.

What we learn so far

AddMvcCore() method adds only the required MVC services to the dependency injection container whereas the AddMvc() method adds all the required MVC services.

  • As the name implies, AddMvcCore() method only adds the core MVC services.
  • On the other hand, AddMvc() method adds all the required MVC services. AddMvc() method calls AddMvcCore() method internally, to add all the core MVC services.
  • So if we are calling AddMvc() method there is no need to explicitly call AddMvcCore() method again.

Video Summary

Conclusion

So far in this article we are discussed the difference between AddMVC() and AddMVCCore(). if have any suggestion for improvement please give your feedback.

Pin It on Pinterest

Share This