如何在asp.net核心中的运行时更改路由集合

时间:2018-10-28 07:23:09

标签: asp.net-mvc model-view-controller .net-core asp.net-routing maproute

我正在为Web应用程序研究ASP.NET core 2.0,并且我使用json文件作为路由数据源,就像这样:

route data source(jsonroute.json):

{
    "RouteSettings": [
        {
            "Name": "ArticleRoute",
            "Template": "{pathroot:regex((?i)^(HelperCenter|xxzx))}/{*pathetc:regex(^.*?(?<!index\\d*(/|\\.html)?)$)}",
            "Default": {
                "Controller": "Article", //articlecontroller is a file of site controllers, is selected when editing article class information
                "Action": "Index"
            }
        },
        {
            "Name": "ArticleClassRoute",
            "Template": "{pathroot:regex((?i)^(HelperCenter|Tours|xxzx))}/{*pathetc:regex((?i)^.*?(?<=index\\d*(/|\\.html)?)$)}",
            "Default": {
                "Controller": "ArticleClass",
                "Action": "Index"
            }
        },
        ....
    ]
}

映射到mvc routebuilder:

app.UseMvc(routeBuilder =>
    {
        foreach (IConfigurationSection routeConfigItem in configurationItems)
        {
            .......
            routeBuilder.MapRoute(
                name: nameSection.Value, //this is form json name node
                template: templateSection.Value, // this is from template node
                defaults: defaultValueDictionary //this is from default node
            );
        }
    }
);

startup.cs上,我从此json文件中读取信息,并将其映射到每个项目的mvc iroutebuilder。 但是有一个问题: json文件来自我网站的文章类别和文章设置,文章类别更改后可能会更改。 所以我想知道有什么办法可以让我在网站在线运行时更改routecollections?我想通过我的网站管理操作来更改路线集合,并使其在线发生。

0 个答案:

没有答案