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

标签 c# asp.net-core asp.net-core-2.1 asp.net-core-2.2

我们已经构建了一个 ASP.NET Core 2.1 网站,其中的 URL 类似于 www.example.org/ukwww.example.org/de确定要显示的 resx 文件和内容。升级到 ASP.NET Core 2.2 后,页面加载但生成的所有链接都生成空白/空 href。

例如,一个链接是这样的:

<a asp-controller="Home" asp-action="Contact">@Res.ContactUs</a>

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

<a href="">Contact us</a>

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

<a href="/uk/contact">Contact us</a>

我们正在使用 Constraint Map管理基于 URL 的语言功能 - 这是代码:

Startup.cs

// 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?}");
}

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

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

httpContext = null
route = {Microsoft.AspNetCore.Routing.NullRouter)

在 2.1 中

 httpContext = {Microsoft.AspNetCore.Http.DefaultHttpContext}
 route = {{lang:lang}/{controller=Home}/{action=Index}/{id?}}

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

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

以下(由于 https://github.com/aspnet/AspNetCore/issues/4206)

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

有什么想法吗?

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

最佳答案

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

关于c# - 升级到 asp.net core 2.2 后为空 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53662496/

相关文章:

c# - WebClient.DownloadDataAsync 正在卡住我的 UI

json - npm - 未安装;路径中的非法字符

c# - 以更好的方式在.net core web api中上传图像

c# - 迁移到 .NET Core 2.1 破坏了 Swagger UI

c# - VS30063 : You are not authorized to access https://dev. 蔚蓝网

c# - 静态类的公共(public)静态成员(属性)是线程安全的

c# - 在 Windows Server 2008 R2 上使用异步套接字导致 100% CPU 使用率

c# - 仅存储键的最佳查找数据结构(没有值的字典)

asp.net-core - 附加到 IIS 中的 MVC 6 进程

c# - 如何将文件和 json 对象从 postman 传递到 asp.net 核心 webapi