Dot.NET 核心 5 路由
Dot.NET Core 5 Routing
我正在尝试路由到一个页面,但它没有像我预期的那样工作。
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("onwhite",
"{ controller=Portfolio}/{action=OnWhite}");
});
我可以很好地访问默认索引页面,利用根域,但是如果我尝试访问 https://www.example.com/onwhite, it fails to locate the page. Consequently, calling https://www.example.com/Portfolio/OnWhite 也可以正常工作。
如果我使用路由端点,如何在 .NET Core 5 中获得我想要的东西?
需要按照从最具体到最不具体的顺序完成控制器端点映射。
这里发生的事情是匹配第一个 ('default') 路由,而 .NET 正在寻找一个名为 'OnwhiteController' 的 class - 它不存在。它甚至没有达到 'onwhite' 路由定义。
您需要定义一个自定义模式 - 'onwhite' - 并指定需要使用哪个控制器和操作来显示该页面。
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "onwhite",
pattern: "onwhite", // this is what matches in the URL
defaults: new { controller = "Portfolio", action = "OnWhite" }
);
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
});
分离路线
此处的UseEndpoints
方法采用单个参数Action<IEndpointRouteBuilder>
。你可以创建一个静态的 class 来保存你所有的路由,这将有助于保持你的 Startup.cs 文件干净:
public static class Endpoints
{
public static void Add(IEndpointRouteBuilder endpoints)
{
endpoints.MapControllerRoute(
name: "onwhite",
pattern: "onwhite", // this is what matches in the URL
defaults: new { controller = "Portfolio", action = "OnWhite" }
);
// add the rest of your endpoints here
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
}
}
然后您可以在 ConfigureServices
中执行此操作:
app.UseEndpoints(Endpoints.Add);
我正在尝试路由到一个页面,但它没有像我预期的那样工作。
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("onwhite",
"{ controller=Portfolio}/{action=OnWhite}");
});
我可以很好地访问默认索引页面,利用根域,但是如果我尝试访问 https://www.example.com/onwhite, it fails to locate the page. Consequently, calling https://www.example.com/Portfolio/OnWhite 也可以正常工作。
如果我使用路由端点,如何在 .NET Core 5 中获得我想要的东西?
需要按照从最具体到最不具体的顺序完成控制器端点映射。
这里发生的事情是匹配第一个 ('default') 路由,而 .NET 正在寻找一个名为 'OnwhiteController' 的 class - 它不存在。它甚至没有达到 'onwhite' 路由定义。
您需要定义一个自定义模式 - 'onwhite' - 并指定需要使用哪个控制器和操作来显示该页面。
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "onwhite",
pattern: "onwhite", // this is what matches in the URL
defaults: new { controller = "Portfolio", action = "OnWhite" }
);
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
});
分离路线
此处的UseEndpoints
方法采用单个参数Action<IEndpointRouteBuilder>
。你可以创建一个静态的 class 来保存你所有的路由,这将有助于保持你的 Startup.cs 文件干净:
public static class Endpoints
{
public static void Add(IEndpointRouteBuilder endpoints)
{
endpoints.MapControllerRoute(
name: "onwhite",
pattern: "onwhite", // this is what matches in the URL
defaults: new { controller = "Portfolio", action = "OnWhite" }
);
// add the rest of your endpoints here
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
}
}
然后您可以在 ConfigureServices
中执行此操作:
app.UseEndpoints(Endpoints.Add);