简体   繁体   English

ASP.NET Core 2从另一个类加载常规路由(路由表)

[英]ASP.NET Core 2 load conventional routes (routing table) from another class

I'm currently working on an ASP.NET Core 2 application. 我目前正在研究ASP.NET Core 2应用程序。 I configure my middleware in the Startup.cs Configure method. 我在Startup.cs Configure方法中配置中间件。 In this Method - Configure - I'm setting my conventional routes as well. 在此方法-配置-我也要设置常规路线。 (I don't need Attribute routing in my application). (我的应用程序中不需要属性路由)。

The problem is, as soon as the application is going to grow, the configuration might become a little bit confusing due to the huge amount of code. 问题是,一旦应用程序增长,由于大量的代码,配置可能会变得有些混乱。

Now I try to figure out, how to load the conventional routing table from an external class. 现在,我尝试找出如何从外部类加载常规路由表。

My current routing table looks very regular like that: 我当前的路由表看起来很常规,如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

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

Now I try to figure out, how to load my routes from another class: 现在,我试图弄清楚如何从另一个类加载我的路由:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        // .. I want to load my conventional routes from an external class
    });
}

I just figured there is a way to create an extension method using the RouteBuilder class, like in this post: 我只是想出了一种使用RouteBuilder类创建扩展方法的方法,就像这篇文章:

How to write a middleware as custom router in asp.net core? 如何在asp.net核心中编写中间件作为自定义路由器?

Hence, in my Configure method I could simply call something like that: 因此,在我的Configure方法中,我可以简单地调用这样的内容:

app.UseMyCoolRouter();

My problem is, I don't really know how to configre the RouteHandler to process regular requests: 我的问题是,我真的不知道如何配置RouteHandler来处理常规请求:

var rh = new RouteHandler(context => 
{
    // ... how to configure for regular routes?
});

var rb = new RouteBuilder(app, rh);

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

app.UseRouter(rb.Build());

When I create the RouteBuilder instance without a RouteHandler: 当我创建没有RouteHandler的RouteBuilder实例时:

var rb = new RouteBuilder(app);

I'm only getting an Null reference exception: 我只得到一个Null引用异常:

"A default handler must be set on the IRouteBuilder." “必须在IRouteBuilder上设置默认处理程序。”

Do you know how I can load my routing table from another class or extension method into my Configure Method in Startup.cs? 您是否知道如何将路由表从另一个类或扩展方法加载到Startup.cs的Configure方法中?

Thank you very much!! 非常感谢你!!

Here is my solution: 这是我的解决方案:

public class Startup
{
   //...    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {   
        app.UseDemoRoutes();
    }
}

public static class ApplicationExtensions
{
    public static IApplicationBuilder UseDemoRoutes(this IApplicationBuilder app)
    {
        app.UseMvc(routes => new DemoRouter(routes));

        return app;
    }
}

public class DemoRouter
{    
    public DemoRouter(IRouteBuilder routes)
    {
        ConfigureRoutes(routes);
        // ConfigureMoreRoutes(routes);
    }

    private void ConfigureRoutes(IRouteBuilder routes)
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    }
}

First I created an extension method clean up the Configure method, next I call a class to configure all my routes. 首先,我创建了一个扩展方法来清理Configure方法,然后调用一个类来配置所有路由。

I don't know if it's the best solution, but it works fine... What do you think? 我不知道这是否是最好的解决方案,但是效果很好...您如何看待? Thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM