我最近更新了我的.Net Core2.1应用程序到3.1,但有一个部分没有按预期升级。
我已经编写了代码来将子域映射到here所描述的区域
我现在意识到使用app.UseMvc()的方法应该被app.UseEndpoints()取代,但我在3.1框架中找不到允许我在app.UseEndpoints()之前写入RouteData的任何地方
//update RouteData before this
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"admin", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
"default", "{controller=Home}/{action=Index}/{id?}");
});有没有一种方法可以使用中间件来写入RouteData?
我试着回到app.UseMvc(),因为它仍然在框架中,但是MvcRouteHandler似乎已经不存在了
app.UseMvc(routes =>
{
routes.DefaultHandler = new MvcRouteHandler(); //The type of namespace could not be found
routes.MapRoute(
"admin",
"{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});发布于 2020-03-23 11:03:42
尝试使用custom middleware。
添加使用Microsoft.AspNetCore.Routing的参考,并使用Httpcontext.GetRouteData()方法实现RouteData
app.UseRouting();
app.Use(async (context, next) =>
{
string url = context.Request.Headers["HOST"];
var splittedUrl = url.Split('.');
if (splittedUrl != null && (splittedUrl.Length > 0 && splittedUrl[0] == "admin"))
{
context.GetRouteData().Values.Add("area", "Admin");
}
// Call the next delegate/middleware in the pipeline
await next();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"admin", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
"default", "{controller=Home}/{action=Index}/{id?}");
});https://stackoverflow.com/questions/60791843
复制相似问题