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.
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.
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.
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.
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.
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.
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.
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 😉
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.
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.
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.
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.
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.
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.
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 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.