c# - .Net Core MVC 中是否有类似 RouteConfig.cs 的东西?

标签 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

最佳答案

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

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

关于c# - .Net Core MVC 中是否有类似 RouteConfig.cs 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56824966/

相关文章:

c# - 在 C# BOL 类中正确实现接口(interface)

c# - 在 ASP.NET 和 XDocument.Load 中获取文件路径

c# - KeyNotFoundException 信息

c# - 如何在 Blazor/Razor 中获取事件数据

iis - 如何在 IIS 网站的子文件夹中部署 ASP.NET Core 应用程序?

c# - .NET Core 2 中间件解决方案中的 ClaimsIdentity 问题

c# - Asp.net core 2前缀路由

c# - 具有多个索引的 ASP.NET 缓存

c# - Url.Action 不适用于使用路由注释在 Controller 中指定的路由

c# - 通过URL传入id时Mvc Route 404