升级到asp.net核心2.2后为空href



我们已经建立了一个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>

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

启动.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?}");
}

LanguageRoutConstraint.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#参数转换器引用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);

与早期版本路由的差异解释了这里发生的事情(重点是我的(:

链接生成环境值无效算法与端点路由一起使用时表现不同。

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

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

在您的示例中,lang环境值,因此在从Home/Index转到Home/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>

您可以通过例如Action Filter来减少重复性,但概念仍然相同。我看不出有其他方法可以处理这一问题(例如,能够将特定值标记为环境值(,但也许其他人也能参与进来。

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

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

最新更新