【问题标题】:Change route collection of MVC6 after startup启动后更改MVC6的路由集合
【发布时间】:2015-12-10 12:17:05
【问题描述】:

在 MVC-5 中,我可以在初始启动后通过访问 RouteTable.Routes 来编辑 routetable。我希望在 MVC-6 中做同样的事情,这样我就可以在运行时添加/删除路由(对 CMS 很有用)。

在 MVC-5 中的代码是:

using (RouteTable.Routes.GetWriteLock())
{
    RouteTable.Routes.Clear();

    RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    RouteTable.Routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

但我在 MVC-6 中找不到 RouteTable.Routes 或类似的东西。知道如何在运行时更改路由集合吗?


我想利用这个原理,例如在CMS中创建页面时,添加一个额外的url。

如果你有这样的课程:

public class Page
{
    public int Id { get; set; }
    public string Url { get; set; }
    public string Html { get; set; }
}

还有一个像这样的控制器:

public class CmsController : Controller
{
    public ActionResult Index(int id)
    {
        var page = DbContext.Pages.Single(p => p.Id == id);
        return View("Layout", model: page.Html);
    }
}

然后,当一个页面被添加到数据库中时,我重新创建了routecollection

var routes = RouteTable.Routes;
using (routes.GetWriteLock())
{
    routes.Clear();
    foreach(var page in DbContext.Pages)
    {
        routes.MapRoute(
            name: Guid.NewGuid().ToString(),
            url: page.Url.TrimEnd('/'),
            defaults: new { controller = "Cms", action = "Index", id = page.Id }
        );
    }

    var defaultRoute = routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

通过这种方式,我可以将不属于约定或严格模板的页面添加到 CMS。我可以添加一个带有 url /contact 的页面,也可以添加一个带有 url /help/faq/how-does-this-work 的页面。

【问题讨论】:

  • 您是否尝试过依赖 IRouteBuilder 以便将其注入您的类中,然后也许您可以使用它来添加与启动时相同的路由?我不知道这是否可行,但这是我会尝试的。
  • @JoeAudette 不,因为我确实查看了源代码,所以路由构建器仅用于“构建”路由一次,之后它被丢弃。所以启动后,builder的使用就没了
  • 查看以下示例,其中使用自定义约定来更改路由:github.com/aspnet/Entropy/tree/dev/samples/…
  • @KiranChalla 不好,那只是开始时定义路由的另一种方式,我想在运行时更改路由
  • @SynerCoder:很好奇,你正在尝试什么场景?

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


【解决方案1】:

答案是没有合理的方法可以做到这一点,即使你找到了方法,这也不是一个好习惯。

解决问题的错误方法

基本上,过去 MVC 版本的路由配置旨在充当 DI 配置 - 也就是说,您将所有内容放在 composition root 中,然后在运行时使用该配置。问题是您可以在运行时将对象推送到配置中(很多人都这样做了),这不是正确的方法。

现在配置已被真正的 DI 容器替换,这种方法将不再有效。注册步骤现在只能在应用程序启动时完成。

正确的方法

自定义路由的正确方法远远超出 Route 类在过去的 MVC 版本中可以做的事情是 inherit RouteBase 或 Route。

AspNetCore(以前称为 MVC 6)具有类似的抽象,IRouterINamedRouter 扮演相同的角色。很像它的前身,IRouter 只有两种方法可以实现。

namespace Microsoft.AspNet.Routing
{
    public interface IRouter
    {
        // Derives a virtual path (URL) from a list of route values
        VirtualPathData GetVirtualPath(VirtualPathContext context);

        // Populates route data (including route values) based on the
        // request
        Task RouteAsync(RouteContext context);
    }
}

此接口用于实现路由的 2 向特性 - URL 路由值和路由值到 URL。

一个例子:CachedRoute<TPrimaryKey>

这是一个跟踪和缓存主键到 URL 的 1-1 映射的示例。它是通用的,我已经测试过无论主键是int 还是Guid,它都有效。

有一个必须注入的可插入部分,ICachedRouteDataProvider,其中可以实现对数据库的查询。您还需要提供控制器和操作,因此此路由足够通用,可以使用多个实例将多个数据库查询映射到多个操作方法。

using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

public class CachedRoute<TPrimaryKey> : IRouter
{
    private readonly string _controller;
    private readonly string _action;
    private readonly ICachedRouteDataProvider<TPrimaryKey> _dataProvider;
    private readonly IMemoryCache _cache;
    private readonly IRouter _target;
    private readonly string _cacheKey;
    private object _lock = new object();

    public CachedRoute(
        string controller, 
        string action, 
        ICachedRouteDataProvider<TPrimaryKey> dataProvider, 
        IMemoryCache cache, 
        IRouter target)
    {
        if (string.IsNullOrWhiteSpace(controller))
            throw new ArgumentNullException("controller");
        if (string.IsNullOrWhiteSpace(action))
            throw new ArgumentNullException("action");
        if (dataProvider == null)
            throw new ArgumentNullException("dataProvider");
        if (cache == null)
            throw new ArgumentNullException("cache");
        if (target == null)
            throw new ArgumentNullException("target");

        _controller = controller;
        _action = action;
        _dataProvider = dataProvider;
        _cache = cache;
        _target = target;

        // Set Defaults
        CacheTimeoutInSeconds = 900;
        _cacheKey = "__" + this.GetType().Name + "_GetPageList_" + _controller + "_" + _action;
    }

    public int CacheTimeoutInSeconds { get; set; }

    public async Task RouteAsync(RouteContext context)
    {
        var requestPath = context.HttpContext.Request.Path.Value;

        if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
        {
            // Trim the leading slash
            requestPath = requestPath.Substring(1);
        }

        // Get the page id that matches.
        TPrimaryKey id;

        //If this returns false, that means the URI did not match
        if (!GetPageList().TryGetValue(requestPath, out id))
        {
            return;
        }

        //Invoke MVC controller/action
        var routeData = context.RouteData;

        // TODO: You might want to use the page object (from the database) to
        // get both the controller and action, and possibly even an area.
        // Alternatively, you could create a route for each table and hard-code
        // this information.
        routeData.Values["controller"] = _controller;
        routeData.Values["action"] = _action;

        // This will be the primary key of the database row.
        // It might be an integer or a GUID.
        routeData.Values["id"] = id;

        await _target.RouteAsync(context);
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        VirtualPathData result = null;
        string virtualPath;

        if (TryFindMatch(GetPageList(), context.Values, out virtualPath))
        {
            result = new VirtualPathData(this, virtualPath);
        }

        return result;
    }

    private bool TryFindMatch(IDictionary<string, TPrimaryKey> pages, IDictionary<string, object> values, out string virtualPath)
    {
        virtualPath = string.Empty;
        TPrimaryKey id;
        object idObj;
        object controller;
        object action;

        if (!values.TryGetValue("id", out idObj))
        {
            return false;
        }

        id = SafeConvert<TPrimaryKey>(idObj);
        values.TryGetValue("controller", out controller);
        values.TryGetValue("action", out action);

        // The logic here should be the inverse of the logic in 
        // RouteAsync(). So, we match the same controller, action, and id.
        // If we had additional route values there, we would take them all 
        // into consideration during this step.
        if (action.Equals(_action) && controller.Equals(_controller))
        {
            // The 'OrDefault' case returns the default value of the type you're 
            // iterating over. For value types, it will be a new instance of that type. 
            // Since KeyValuePair<TKey, TValue> is a value type (i.e. a struct), 
            // the 'OrDefault' case will not result in a null-reference exception. 
            // Since TKey here is string, the .Key of that new instance will be null.
            virtualPath = pages.FirstOrDefault(x => x.Value.Equals(id)).Key;
            if (!string.IsNullOrEmpty(virtualPath))
            {
                return true;
            }
        }
        return false;
    }

    private IDictionary<string, TPrimaryKey> GetPageList()
    {
        IDictionary<string, TPrimaryKey> pages;

        if (!_cache.TryGetValue(_cacheKey, out pages))
        {
            // Only allow one thread to poplate the data
            lock (_lock)
            {
                if (!_cache.TryGetValue(_cacheKey, out pages))
                {
                    pages = _dataProvider.GetPageToIdMap();

                    _cache.Set(_cacheKey, pages,
                        new MemoryCacheEntryOptions()
                        {
                            Priority = CacheItemPriority.NeverRemove,
                            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(this.CacheTimeoutInSeconds)
                        });
                }
            }
        }

        return pages;
    }

    private static T SafeConvert<T>(object obj)
    {
        if (typeof(T).Equals(typeof(Guid)))
        {
            if (obj.GetType() == typeof(string))
            {
                return (T)(object)new Guid(obj.ToString());
            }
            return (T)(object)Guid.Empty;
        }
        return (T)Convert.ChangeType(obj, typeof(T));
    }
}

CmsCachedRouteDataProvider

这是数据提供者的实现,基本上是您需要在 CMS 中执行的操作。

public interface ICachedRouteDataProvider<TPrimaryKey>
{
    IDictionary<string, TPrimaryKey> GetPageToIdMap();
}

public class CmsCachedRouteDataProvider : ICachedRouteDataProvider<int>
{
    public IDictionary<string, int> GetPageToIdMap()
    {
        // Lookup the pages in DB
        return (from page in DbContext.Pages
                select new KeyValuePair<string, int>(
                    page.Url.TrimStart('/').TrimEnd('/'),
                    page.Id)
                ).ToDictionary(pair => pair.Key, pair => pair.Value);
    }
}

用法

这里我们在默认路由之前添加路由,并配置其选项。

// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
    routes.Routes.Add(
        new CachedRoute<int>(
            controller: "Cms",
            action: "Index",
            dataProvider: new CmsCachedRouteDataProvider(), 
            cache: routes.ServiceProvider.GetService<IMemoryCache>(), 
            target: routes.DefaultHandler)
        {
            CacheTimeoutInSeconds = 900
        });

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

    // Uncomment the following line to add a route for porting Web API 2 controllers.
    // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
});

这就是它的要点。你仍然可以改进一点。

例如,我个人会使用工厂模式并将存储库注入CmsCachedRouteDataProvider 的构造函数,而不是到处硬编码DbContext

【讨论】:

  • 我必须对此进行调查,但它看起来很有希望,也许在我研究这条途径时,我有一个解决您问题的方法:)。但是今晚我有客人,所以我可能明天会去看看。但就目前而言,看起来很有希望。 +1
  • @SynerCoder - 我收到了为什么我的代码失败的回复,并更新了我的答案。
  • @nightowl888 我知道这是旧的,但“context.isBound”和“context.IsHandled”在 1.1(也许 1)中不存在。你知道它们被替换成什么(如果有的话)吗?
  • 我正在尝试理解你的代码但没有得到一些东西。你能帮我理解吗??
  • 我已经更新了使用 .NET Core 2.0 的答案。如果还有什么不明白的地方请告诉我。
【解决方案2】:

一种简单的方法是,如果发生 404 错误,则检查以下问题:

如果路由列表中存在 URL,则重定向到该路由列表

sample .net core 转到 Startup.cs(project root) 然后在 Configure 方法中添加底部代码:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
    app.UseStatusCodePages(async context =>
                    {
                        var redirctPage = pageToRedirect(context);
                        context.HttpContext.Response.Redirect(redirctPage);

                    }
...
}
    private string pageToRedirect(StatusCodeContext context)
    {
        var def = "";
        if(context.HttpContext.Response.StatusCode==404){
            if (context.HttpContext.Request.Path.ToString().ToLower().Contains("/product/"))
            {
                def = "/Home/Product";
                def += context.HttpContext.Request.QueryString;
            }
            else if (context.HttpContext.Request.Path.ToString().ToLower()=="/news")//or you can call class that load info from DB to redirect
            {
                def = "/Home/News";
                def += context.HttpContext.Request.QueryString;
            }
            else//404 error page
                def = "/Home/Error?statusCode=" + context.HttpContext.Response.StatusCode;
        }else //other errors code
            def = "/Home/Error?statusCode=" + context.HttpContext.Response.StatusCode;

        return def;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多