Boj*_*jan 7 c# routing asp.net-core-3.0 .net-core-3.0
我试图弄清楚如何正确替换曾经是app.UseMvc().net core 2.2 的一部分的代码。这些示例甚至告诉我我可以调用的所有代码是什么,但我还不明白我应该调用哪个。例如,对于我的 MVC Web 应用程序,我有以下内容:
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?}");
});
}
Run Code Online (Sandbox Code Playgroud)
在我在UseMvc()选项中提供我的路由之前。不过现在看来我必须在里面提供它MapControllerRoute但是示例似乎总是也调用MapRazorPages(). 我需要打电话给两个还是我想只打电话给一个?两者之间的实际区别是什么,如何设置默认控制器和默认操作?
Rua*_*urg 14
这在从 ASP.NET Core 2.2 迁移到 3.0 一文中有所记录。假设您想要一个 MVC 应用程序。
以下示例添加了对控制器、API 相关功能和视图的支持,但不支持页面。
services
// more specific than AddMvc()
.AddControllersWithViews()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
Run Code Online (Sandbox Code Playgroud)
在配置中:
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?}");
});
}
Run Code Online (Sandbox Code Playgroud)
有关使用语句的顺序,请查看文档。