gpt4 book ai didi

c# - 如何在 asp.net core 中将路由配置与启动分开?

转载 作者:太空狗 更新时间:2023-10-30 00:37:24 24 4
gpt4 key购买 nike

我想在 asp.net core 中将路由配置与启动分开?默认情况下在 .net 核心中:

            app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

最佳答案

你可以使用下面的代码:

public static class RouteConfig
{
public static IRouteBuilder Use(IRouteBuilder routeBuilder)
{
//eg sample for defining Custom route
//routeBuilder.MapRoute("blog", "blog",
// defaults: new { controller = "Home", action = "Index" });

routeBuilder.MapRoute(name: "default",
template: "{controller=Home}/{action=Index}/{id?}");

return routeBuilder;
}
}

在启动和配置方法中:

app.UseMvc(c => RouteConfig.Use(c));

关于c# - 如何在 asp.net core 中将路由配置与启动分开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51818461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com