简体   繁体   English

在IRouter实现ASP.NET Core MVC上获取404

[英]Getting 404 on IRouter implementation ASP.NET Core MVC

I'm trying to implement my own dynamic router, my plan is to pull routes from my database and create a set of dynamic landing pages, my issue is that I'm getting 404 after setting up context.RouteData to my new route data. 我正在尝试实现自己的动态路由器,我的计划是从数据库中提取路由并创建一组动态登陆页面,我的问题是,在设置context.RouteData之后,我得到了404到新的路由数据。

I just want to redirect to my LandingPageController and the Index IActionResult everytime I found a route. 每当我找到一条路线时,我只想重定向到我的LandingPageController和Index IActionResult。

using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using System.Collections.Generic;
using System.Linq;

namespace Myproject.Web.Main.Config
{
    public class LandingPageRouter : IRouter
    {
        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            return null;
        }

        public Task RouteAsync(RouteContext context)
        {

            var requestPath = context.HttpContext.Request.Path.Value;
            if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
            {
                requestPath = requestPath.Substring(1);
            }

            var pagefound = GetPages().Any(x => x == requestPath);
            if (pagefound)
            {
                //TODO: Handle querystrings
                var routeData = new RouteData();
                routeData.Values["controller"] = "LandingPage";
                routeData.Values["action"] = "Index";
                context.RouteData = routeData;
            }

            return Task.FromResult(0);
        }

        private IEnumerable<string> GetPages()
        {
            //TODO: pull from database
            return new List<string> { "page-url-title", "another-dynamic-url" };
        }
    }
}

I looked at this answer but it seems outdated some properties in the context doesn't even exist anymore in RC2. 我看了这个答案,但是在RC2中,上下文中的某些属性似乎已经过时了。

What Am I missing? 我想念什么?

It doesn't seem like the nicest solution but from my testing it should work for your requirements. 这似乎不是最好的解决方案,但从我的测试来看,它应该可以满足您的要求。


I injected the default MVC IRouter into your LandingPageRouter . 我将默认的MVC IRouter注入了您的LandingPageRouter Then, once you have updated the route data, just call the default router and pass in the context: 然后,一旦更新了路由数据,只需调用默认路由器并传递上下文即可:

public class LandingPageRouter : IRouter
{
    private readonly IRouter _router;

    public LandingPageRouter(IRouter router)
    {
        _router = router;
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        return null;
    }

    public Task RouteAsync(RouteContext context)
    {

        var requestPath = context.HttpContext.Request.Path.Value;
        if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
        {
            requestPath = requestPath.Substring(1);
        }

        var pagefound = GetPages().Any(x => x == requestPath);
        if (pagefound)
        {
            //TODO: Handle querystrings
            var routeData = new RouteData();
            routeData.Values["controller"] = "LandingPage";
            routeData.Values["action"] = "Index";
            context.RouteData = routeData;
            return _router.RouteAsync(context);
        }

        return Task.FromResult(0);
    }

    private IEnumerable<string> GetPages()
    {
        //TODO: pull from database
        return new List<string> { "page-url-title", "another-dynamic-url" };
    }
}

Then just insert the default route wherever you are adding your route in Startup.Configure : 然后,只要在Startup.Configure中添加路由的地方插入默认路由即可:

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

Based on this answer and @sock approach, I passed the route builder which is going to pass at the same time the router context 基于此答案和@sock方法,我通过了路由构建器,该构建器将在路由器上下文同时传递

public LandingPageRouter(IRouteBuilder routeBuilder)
{
    _routeBuilder = routeBuilder;
}

public Task RouteAsync(RouteContext context)
{
    var requestPath = context.HttpContext.Request.Path.Value;
    if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
    {
        requestPath = requestPath.Substring(1);
    }

    var pagefound = GetPages().SingleOrDefault(x => x.Name == requestPath);
    if (pagefound!=null)
    {
        //TODO: Handle querystrings
        var routeData = new RouteData();
        routeData.Values["controller"] = "LandingPage";
        routeData.Values["action"] = "Index";
        routeData.Values["id"] = pagefound.Id;

        context.RouteData = routeData;
        return _routeBuilder.DefaultHandler.RouteAsync(context);
    }
    return Task.FromResult(0);
}

Routes configuration in startup.cs 在startup.cs中的路由配置

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

    routes.Routes.Add(new LandingPageRouter(routes));
}

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

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