如何将路由配置与asp.net核心中的启动分开?

时间:2018-08-13 08:48:50

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

我想将路由配置与asp.net核心中的启动分开吗? 在.net核心中默认为:

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

1 个答案:

答案 0 :(得分:6)

您可以使用以下代码:

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));