Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Any way to access the RouteData from Middleware before Routing is handling it? #4952

Closed
@TsengSR

Description

@TsengSR

Is there any way to register the routings before Mvc Middleware is registered?

The use case is following: I want to use custom localization in url and for that I need to write a customized RequestCultureProvider.

public class UrlRequestCultureProvider : RequestCultureProvider
{
    public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentNullException(nameof(httpContext));
        }

        RouteData routes = httpContext.GetRouteData();
        var culture = routes?.Values["culture"]?.ToString();
        if (!string.IsNullOrWhiteSpace(culture))
        {
            return Task.FromResult(new ProviderCultureResult(culture));
        }

        return Task.FromResult<ProviderCultureResult>(null);
    }
}

Thie problem here is, that httpContext.GetrouteData() returns null, because the routings are not built yet, only after the call to UseMvc() call. But registering the localization after this point is already to late to determine the locale.

I've seen that GetRouteData basically just returns the data from the IRoutingFeature implementation, which is added when UseRoute() is being called inside UseMvc(). Do we have a way to register the routings before the call to UseMvc()?

None of the Mvc extensions methods allows us to skip the app.UseRoute(routes.Build()) part.

The only option I have now is to "parse" (i.e. string split) the request url myself, which may work in simple cases where the culture is in the format of /api/{culture:culture}/{controller}/{action} but may not work on more complex requirements.

So it would be nice to have a method to have an intercepting middleware, one that is called after the IRoutingFeature has been set, but before the actually controller/action method is being invoked

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions