c# – 升级到asp.net core 2.2后清空href
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 升级到asp.net core 2.2后清空href,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2898字,纯文字阅读大概需要5分钟。
内容图文
![c# – 升级到asp.net core 2.2后清空href](/upload/InfoBanner/zyjiaocheng/822/45b572f44bfb4c7f8c8b35ff70fb9e14.jpg)
我们构建了一个ASP.NET Core 2.1网站,其中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匹配方法中没有设置一些参数,例如,
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://docs.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全部内容,希望文章能够帮你解决c# – 升级到asp.net core 2.2后清空href所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。