.Net Core MVC中是否有类似RouteConfig.cs的内容?

时间:2019-06-30 12:48:30

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

我正在尝试阅读亚当·弗里曼(Adam Freeman)的书“ ASP .NET MVC”。在本书的一章中,作者建议将路由放置到特殊配置文件App_Start/RouteConfig.cs中。看起来不错,但是我正尝试在.Net Core的帮助下实现它。我没有找到路线的特殊地方,所以将路线放入了Startup.cs中。但这看起来很丑。也许有人知道这种情况下的优雅解决方案?

这是我的Startup.cs

的代码
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // services are here ..
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            //app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
            app.UseHttpsRedirection();
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            // /
            routes.MapRoute(null, "", new
            {
                controller = "Products",
                action = "List",
                category = "", page = 1
            });

            // Page2
            routes.MapRoute(null,
                "Page{page}",
                new
                {
                    controller = "Products",
                    action = "List",
                    category = ""
                },
                new { page = @"\d+" }
            );

            // Category
            routes.MapRoute(null,
                "{category}",
                new
                {
                    controller = "Products",
                    action = "List",
                    page = 1
                });

            // Category/Page2
            routes.MapRoute(null,
                "{category}/Page{page}",
                new
                {
                    controller = "Products",
                    action = "List",
                },
                new
                {
                    page = @"\d+"
                });
        });
    }
}

P.S .Net Core版本是2.2

1 个答案:

答案 0 :(得分:3)

您可以将它们放在另一个文件中:

public static class Routing
{
    public static void Include(IApplicationBuilder app)
    {
        app.UseMvc(routes =>
        {
            // /
            routes.MapRoute(null, "", new
            {
                controller = "Products",
                action = "List",
                category = "",
                page = 1
            });

            // Page2
            routes.MapRoute(null,
                "Page{page}",
                new
                {
                    controller = "Products",
                    action = "List",
                    category = ""
                },
                new { page = @"\d+" }
            );
        }
        );
    }
}

然后在“启动”类中调用它:

public class Startup
{
    ...
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
        app.UseStaticFiles();
        Routing.Include(app);
        ...
    }
}