升级到asp.net core 2.2

时间:2018-12-07 02:42:57

标签: 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>

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

有什么想法吗?

更新 根据{{​​3}},ASP.NET Core 2.2使用EndpointRouting,而2.1使用IRouter基本逻辑。那解释了我的问题。现在,我的问题是,使用新的EndpointRouting的2.2代码将是什么样?

3 个答案:

答案 0 :(得分:3)

Differences from earlier versions of routing解释了这里发生的事情(强调我的意思):

  

链接生成环境值失效算法与端点路由一起使用时的行为有所不同。

     

环境值无效是一种算法,用于确定可以将当前执行请求中的哪些路由值(环境值)用于链接生成操作。当链接到其他操作时,常规路由总是使多余的路由值无效。在ASP.NET Core 2.2发行之前,属性路由没有此行为。在早期版本的ASP.NET Core中,链接到使用相同路由参数名称的另一个操作会导致链接生成错误。在ASP.NET Core 2.2或更高版本中,两种形式的路由在链接到另一个操作时都会使值无效。

     

...

     

当链接的目标是不同的操作或页面时,不会重用环境值。

在您的示例中,lang是一个环境值,因此从Home/IndexHome/About(不同的操作)时,它不会被重用。如果没有为lang指定值,则不会执行任何匹配操作,因此会生成一个空的href。在文档中这也被描述为端点路由差异:

  

但是,如果该动作不存在,则端点路由会生成一个空字符串。从概念上讲,如果该动作不存在,则端点路由不会假定该端点存在。

如果您想继续使用端点路由,则似乎需要将lang值从控制器传递到视图中,然后进行显式设置。这是一个示例:

public class HomeController : Controller
{
    public IActionResult Index(string lang)
    {
        ViewData["lang"] = lang; // Using ViewData just for demonstration purposes.
        return View();
    }
}
<a asp-controller="Home" asp-action="Contact"
    asp-route-lang="@ViewData["lang"]">@Res.ContactUs</a>

您可以使用例如一个动作过滤器,但概念仍然相同。我看不到有另一种方法可以处理此问题(例如,能够将特定值标记为环境值),但也许其他人也可以对此进行了解。

答案 1 :(得分:3)

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

答案 2 :(得分:0)

您需要显式传递路径数据中的值:

@using Microsoft.AspNetCore.Routing;
<a ... asp-route-storeId="@this.Context.GetRouteValue("storeId")">Pay Button</a>
相关问题