I'm trying to find out why in all docs I see services.AddMvc
or services.AddMvcCore
in Startup.cs
but in ASP.NET Core MVC 3.1 template that's created by VS I do not have AddMvc but mvc still works....
the only thing I have related t MVC is
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
can anyone explain why?
Adds MVC services to the specified IServiceCollection. C# Copy. public static Microsoft.Extensions.DependencyInjection.
If you want to develop a Model View Controller i.e. MVC application then you need to use AddControllersWithViews() method. Further, if you want Pages features into your MVC application, then you need to use the AddMVC method.
AddRazorPages(IServiceCollection) Adds services for pages to the specified IServiceCollection. AddRazorPages(IServiceCollection, Action<RazorPagesOptions>) Adds services for pages to the specified IServiceCollection.
The AddController method Registers everything that is needed for Web API Development. The services include Support for Controllers, Model Binding, API Explorer, Authorization, CORS, Validations, Formatter Mapping, etc.
As the doc has said that asp.net core 3.0+ template use these new
methodsAddControllersWithViews
,AddRazorPages
,AddControllers
instead of AddMvc
.
However, AddMvc
continues to behave as it has in previous releases.AddMvc()
is really just a wrapper around a bunch of other methods that register services. See Source:
https://github.com/aspnet/AspNetCore/blob/0303c9e90b5b48b309a78c2ec9911db1812e6bf3/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs#L27
You could either use AddMvc
to register for MVC, Razor Pages,API or use individual AddControllersWithViews
(for MVC only) and AddRazorPages
(for Razor Pages only).
I think all the docs you are seeing is for .net core 2.*.
For More Information -> https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With