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

标签 c# asp.net-core .net-core asp.net-core-2.0

我想在 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/

相关文章:

c# - 使用 Swashbuckle 将文本部分添加到 Swagger

c# - 迁移到 ASP.NET Core RC2 后集成测试中断

c# - 为什么 .NET Core 选项的配置不适用于泛型类型参数?

c# - 通过 c#/dotnet 核心中的命名管道卸载到 ffmpeg

c# MySql.Data 未加载

c# - 按索引列表搜索?

c# - 如何在ConfigureServices方法中获取IOptions?

c# - 当源为空时如何防止 AutoMapper 初始化目标 JObject 成员

c# - 在 C# 中调用具有多个参数的表达式时出现 NullReferenceException

.net-core - 从 Blazor 应用程序中的中间件类获取经过身份验证的用户