【问题标题】:Is there something like RouteConfig.cs in .Net Core MVC?.Net Core MVC 中有类似 RouteConfig.cs 的东西吗?
【发布时间】:2019-06-30 12:48:30
【问题描述】:

我正在尝试遵循 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

【问题讨论】:

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


    【解决方案1】:

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

    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+" }
                );
            }
            );
        }
    }
    

    然后在 `Startup' 类中调用它:

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

    【讨论】:

    • 也可以将其转换为扩展方法,因为该类是静态的。
    • 当然,只需将方法转换为:public static void IncludeRoutes(this IApplicationBuilder app) 并调用app.IncludeRoutes();
    • 但它隐藏在我使用app.UseMvc() 的内部,这个文件也可以变得非常大。只有这样吗?
    • 这不是为了减小文件的大小,它有助于使Startup 文件看起来更好且易于管理。
    • 我宁愿写一个相应的扩展方法:public static void UseMvcWithRoutes(this IApplicationBuilder app) 并调用app.UseMvcWithRoutes()
    猜你喜欢
    • 2012-11-27
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2021-08-04
    • 2012-05-31
    相关资源
    最近更新 更多