升级到asp.net core 2.2后为空href

Sha*_*Sha 8 c# asp.net-core asp.net-core-2.1 asp.net-core-2.2

我们已经建立了一个ASP.NET Core 2.1网站,其中的URL(例如www.example.org/uk和www.example.org/de)可以确定resx要显示的文件和内容。升级到ASP.NET Core 2.2后,页面会加载,但是生成的所有链接都会产生空白/空的href。

例如,以下链接:

<a asp-controller="Home" asp-action="Contact">@Res.ContactUs</a>
Run Code Online (Sandbox Code Playgroud)

将在2.2中产生一个空的href,如下所示:

<a href="">Contact us</a>
Run Code Online (Sandbox Code Playgroud)

但是在2.1中,我们得到了正确的href:

<a href="/uk/contact">Contact us</a>
Run Code Online (Sandbox Code Playgroud)

我们正在使用约束图来管理基于URL的语言功能-这是代码:

启动文件

// configure route options {lang}, e.g. /uk, /de, /es etc
services.Configure<RouteOptions>(options =>
{
    options.LowercaseUrls = true;
    options.AppendTrailingSlash = false;
    options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint));
 });

 ...

app.UseMvc(routes =>
{
    routes.MapRoute(
       name: "LocalizedDefault",
       template: "{lang:lang}/{controller=Home}/{action=Index}/{id?}");
}
Run Code Online (Sandbox Code Playgroud)

LanguageRouteConstraint.cs

public class LanguageRouteConstraint : IRouteConstraint
{
    private readonly AppLanguages _languageSettings;

    public LanguageRouteConstraint(IHostingEnvironment hostingEnvironment)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        IConfigurationRoot configuration = builder.Build();

        _languageSettings = new AppLanguages();
        configuration.GetSection("AppLanguages").Bind(_languageSettings);
    }

    public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (!values.ContainsKey("lang"))
        {
            return false;
        }

        var lang = values["lang"].ToString();
        foreach (Language lang_in_app in _languageSettings.Dict.Values)
        {
            if (lang == lang_in_app.Icc)
            {
                return true;
            }
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我缩小了问题的范围,但找不到解决方法。基本上在2.2。在上述IRouteConstraint Match方法中未设置某些参数,例如

httpContext = null
route = {Microsoft.AspNetCore.Routing.NullRouter)
Run Code Online (Sandbox Code Playgroud)

在2.1

 httpContext = {Microsoft.AspNetCore.Http.DefaultHttpContext}
 route = {{lang:lang}/{controller=Home}/{action=Index}/{id?}}
Run Code Online (Sandbox Code Playgroud)

我在2.1和2.2之间所做的唯一区别是我更改了

var builder = new ConfigurationBuilder()
   .SetBasePath(Directory.GetCurrentDirectory())
   .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Run Code Online (Sandbox Code Playgroud)

到以下内容(由于 https://github.com/aspnet/AspNetCore/issues/4206

 var builder = new ConfigurationBuilder()
    .SetBasePath(hostingEnvironment.ContentRootPath) // using IHostingEnvironment 
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

根据https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/routing?view=aspnetcore-2.2#parameter-transformer-reference进行更新 ASP.NET Core 2.2使用EndpointRouting,而2.1使用IRouter基本逻辑。那解释了我的问题。现在,我的问题是,使用新的EndpointRouting的2.2代码会是什么样?

Diy*_*yar 6

// Use the routing logic of ASP.NET Core 2.1 or earlier:
services.AddMvc(options => options.EnableEndpointRouting = false)
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Run Code Online (Sandbox Code Playgroud)