• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 23, 2020

Difference Between AddMvc and AddMvcCore

 Admin     August 23, 2020     .Net, .Net Core, .Net Core MVC, Asp.Net, C#     No comments   

In this article, I am going to discuss the Difference Between AddMvc and AddMvcCore Method in the ASP.NET Core application. I strongly recommended you read our previous article before proceeding to this article where we discussed the use of the AddMvc method to Setup the 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.

We are going to work with the same example that we created in our previous article. In our previous article, we use the AddMvc() method. Before going to understand the difference between AddMvc and AddMvcCore method, let us first do some changes into our HomeController.

Let us have a look at the HomeController class.
Difference Between AddMvc and AddMvcCore Method in ASP.NET Core application

As you can see in the above image, 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.

Returning JSON Data from Action Method:
So let’s modify the HomeController class to return JsonResult from the Index action method. First import the Microsoft.AspNetCore.Mvc namespace and then inherit the HomeController class from the base Controller class as shown below.
using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCApplication.Controllers
{
    public class HomeController : Controller
    {
        public JsonResult Index()
        {
            return Json(new { Name = "James", Department = "IT", Salary = 25000 });
        }
    }
}
Following is the code of our Startup class. As you can see, here we are using the AddMvc() method to configure the MVC services to the dependency injection container.
using AddMvc() Method

Now run the application and navigate to the root URL of your application. You will see the JSON data as expected in the browser.

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.
using AddMvcCore() Method

With the above changes in the ConfigureServices() method, now run the application and navigate to the root URL of the application. You will get the following error.
Difference Between AddMvc() and AddMvcCore() Method

The above error clearly states that there is no JSON Formatter registered with the dependency injection container in order to return JSON data. We got this error when we use the AddMvcCore() method but not with the AddMvc() method. The reason is the AddMvc() method registered the Json Formatters within the dependency injection container but not the AddMvcCore() method.

As asp.net core is open source, so you can see 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

Please have a look at the source code of the AddMvc() method.
source code of AddMvc() method

Now let’s have a look at the AddMvcCore() method source code by visiting the following GitHub page.

https://github.com/aspnet/Mvc/blob/1521f9298bc44e70d0fc5f9bc0814e101bbcc479/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs

Please have a look at the source code of the AddMvcCore() method.
source code of AddMvcCore() method

So the difference is that, the AddMvcCore() method adds only the required MVC services to the dependency injection container whereas the AddMvc() method adds all the required MVC services. As you can see from the source code of the AddMvc() method, it internally calls the AddMvcCore() method to add all the core services which are required for MVC.

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 class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var bilder = services.AddMvcCore();
        bilder.AddJsonFormatters();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseMvcWithDefaultRoute();
    }
}
With the above changes, now run the application and you will see the output as expected.

In the next article, I am going to discuss working with Models in the ASP.NET Core MVC application. Here, in this article, I try to explain the Difference Between AddMvc and AddMvcCore method in ASP.NET Core application step by step with an example. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Summary:
I hope this post will be helpful to understand the concept of Difference Between AddMvc and AddMvcCore methods
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape