I'm trying to figure out how to properly replace app.UseMvc()
code that use to be part .net core 2.2. The examples go so far as to tell me what are all the codes I can call but I'm not yet understanding which should I call. For example for my MVC Web Application I have the following:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/Index");
app.UseMiddleware<ExceptionHandler>();
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
context.Context.Response.GetTypedHeaders()
.CacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromDays(30)
};
}
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
Before I would provide my routing inside the UseMvc()
options. However now it seems I have to provide it inside MapControllerRoute
But the examples always seem to also call MapRazorPages()
. Do I need to call both or am I suppose to call just one? What is the actual difference between the two and how do I setup a default controller and a default action?
This is documented in the Migrate from ASP.NET Core 2.2 to 3.0 article. Assuming you want an MVC application.
The following example adds support for controllers, API-related features, and views, but not pages.
services
// more specific than AddMvc()
.AddControllersWithViews()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
And in Configure:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseRouting();
// The equivalent of 'app.UseMvcWithDefaultRoute()'
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
// Which is the same as the template
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
For the order of use statemtents check the documentation.