替换 .Net Core 3.0 中的 UseMvc
Replacing UseMvc in .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?}");
});
}
之前我会在 UseMvc()
选项中提供我的路由。但是现在看来我必须在 MapControllerRoute
中提供它,但是示例似乎总是也调用 MapRazorPages()
。我需要同时调用两者还是只调用一个?两者之间的实际区别是什么?如何设置默认控制器和默认操作?
Migrate from ASP.NET Core 2.2 to 3.0 文章中对此进行了记录。假设您想要一个 MVC 应用程序。
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)
并在配置中:
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?}");
});
}
有关使用语句的顺序,请检查 documentation。
最简单的修复方法...
构建一个针对您需要的 .NET Core 的新项目,只需复制新的 Configure 方法并粘贴到您要迁移到的项目中...
在这个例子中...
以下是旧代码行:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
这里是新的代码行:
// This method gets called by the runtime. Use this method to configure
the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我正在尝试弄清楚如何正确替换 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?}");
});
}
之前我会在 UseMvc()
选项中提供我的路由。但是现在看来我必须在 MapControllerRoute
中提供它,但是示例似乎总是也调用 MapRazorPages()
。我需要同时调用两者还是只调用一个?两者之间的实际区别是什么?如何设置默认控制器和默认操作?
Migrate from ASP.NET Core 2.2 to 3.0 文章中对此进行了记录。假设您想要一个 MVC 应用程序。
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)
并在配置中:
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?}");
});
}
有关使用语句的顺序,请检查 documentation。
最简单的修复方法... 构建一个针对您需要的 .NET Core 的新项目,只需复制新的 Configure 方法并粘贴到您要迁移到的项目中...
在这个例子中... 以下是旧代码行:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
这里是新的代码行:
// This method gets called by the runtime. Use this method to configure
the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}