简体   繁体   English

Asp.Net Core中的自定义IRouter没有任何反应

[英]Nothing happened with custom IRouter in Asp.Net Core

I have the following implementation of my router: 我的路由器具有以下实现:

public class TenantUrlResolverRouter : IRouter
{
    private readonly IRouter _defaultRouter;

    public TenantUrlResolverRouter(IRouter defaultRouter)
    {
        _defaultRouter = defaultRouter;
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        return _defaultRouter.GetVirtualPath(context);
    }

    public async Task RouteAsync(RouteContext context)
    {
        var oldRouteData = context.RouteData;
        var newRouteData = new RouteData(oldRouteData);
        newRouteData.Values["library"] = "Default";
        try
        {
            context.RouteData = newRouteData;
            await _defaultRouter.RouteAsync(context);
        }
        finally
        {
            if (!context.IsHandled)
            {
                context.RouteData = oldRouteData;
            }
        }
    }
}

Then I define it in Startup.cs : 然后在Startup.cs定义它:

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

But nothing happens, RouteData.Values in RouteContext always empty, I always have Unknown , while it's need to be Default . 但是什么也没发生, RouteContext RouteData.Values始终为空,我始终为Unknown ,而它需要为Default That's not the problem of the predefined template, because it even worked without the {library=Unknown} and this {library}/{controller=Home}/{action=Index}/{id?} doesn't work too. 这不是预定义模板的问题,因为它甚至在没有{library=Unknown}情况下也可以工作,并且此{library}/{controller=Home}/{action=Index}/{id?}也不起作用。

What's the problem with this custom IRouter? 此自定义IRouter有什么问题?

You are not providing a complete set of route values. 您没有提供完整的路由值集。 In order to route to an action, you need to provide both controller and action . 为了路由到动作,您需要同时提供controlleraction

Also, you don't have a match condition in the route. 另外,您的路线中没有匹配条件。 A match condition will determine whether the incoming request matches the current route. 匹配条件将确定传入的请求是否与当前路由匹配。 In the built-in routing, the url and constraints are match conditions. 在内置路由中, urlconstraints是匹配条件。 However, in a custom route, you need to put an if block to ensure that any request that doesn't match will pass through to the next registered route. 但是,在自定义路由中,您需要放置一个if块,以确保任何不匹配的请求都将传递到下一条已注册的路由。

NOTE: This is the most powerful part of custom routing. 注意:这是自定义路由最强大的部分。 The built-in routing can only match against URLs. 内置路由只能与URL匹配。 But with custom routing, you can match anything in the request. 但是使用自定义路由,您可以匹配请求中的任何内容。 For example, you could make the route match only for a certain domain or a certain subdomain. 例如,您可以使路由仅对某个域或某个子域匹配。 You could even make it match things like posted form values or session state. 您甚至可以使其与发布的表单值或会话状态之类的东西匹配。

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

    var segments = requestPath.Split('/');

    // Required: Match condition to determine if the incoming request
    // matches this route. If not, you should allow the framework to
    // match another route by doing nothing here.
    if (segments.Length > 0 && segments[0].Equals("libraryname", StringComparison.OrdinalIgnoreCase))
    {
        var oldRouteData = context.RouteData;
        var newRouteData = new RouteData(oldRouteData);
        newRouteData.Values["library"] = segments[0];
        newRouteData.Values["controller"] = segments[1];
        newRouteData.Values["action"] = segments[2];
        try
        {
            context.RouteData = newRouteData;
            await _defaultRouter.RouteAsync(context);
        }
        finally
        {
            if (!context.IsHandled)
            {
                context.RouteData = oldRouteData;
            }
        }
    }
}

See this question for more info about implementing IRouter . 这个问题有关实现更多信息IRouter

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

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