I noticed that last week .NET Core 2.2 Preview 3 was released:
我注意到上周发布了.NET Core 2.2 Preview 3:
You can download and get started with .NET Core 2.2, on Windows, macOS, and Linux:
您可以在Windows,macOS和Linux上下载并开始使用.NET Core 2.2:
.NET Core 2.2 Preview 3 SDK (includes the runtime)
.NET Core 2.2 Preview 3 SDK (包括运行时)
Docker images are available at microsoft/dotnet for .NET Core and ASP.NET Core.
Docker映像在Microsoft / dotnet上可用于.NET Core和ASP.NET Core。
.NET Core 2.2 Preview 3 can be used with Visual Studio 15.9 Preview 3 (or later), Visual Studio for Mac and Visual Studio Code.
.NET Core 2.2 Preview 3可以与Visual Studio 15.9 Preview 3(或更高版本) ,Visual Studio for Mac和Visual Studio Code一起使用。
The feature I am most stoked about in ASP.NET 2.2 is a subtle one but I remember implementing it manually many times over the last 10 years. I'm happy to see it nicely integrated into ASP.NET Core's MVC and Razor Pages patterns.
我在ASP.NET 2.2中最着急的功能是一个微妙的功能,但是我记得在过去的十年中多次手动实现它。 我很高兴看到它很好地集成到ASP.NET Core的MVC和Razor Pages模式中。
ASP.NET Core 2.2 introduces the concept of Parameter Transformers to routing. Remember there isn't a directly relationship between what's in the URL/Address bar and what's on disk. The routing subsystem handles URLs coming in from the client and routes them to Controllers, but it also generates URLs (strings) when given an Controller and Action.
ASP.NET Core 2.2引入了参数转换器的概念 路由。 请记住,URL /地址栏中的内容与磁盘上的内容之间没有直接关系。 路由子系统处理从客户端传入的URL,并将其路由到Controller,但是当给定Controller和Action时,它还会生成URL(字符串)。
So if I'm using Razor Pages and I have a file Pages/FancyPants.cshtml I can get to it by default at /FancyPants. I can also "ask" for the URL when I'm creating anchors/links in my Razor Page:
因此,如果我使用的是Razor Pages,并且有一个文件Pages / FancyPants.cshtml,则默认情况下可以在/ FancyPants上找到它。 在剃刀页面中创建锚点/链接时,我也可以“询问” URL:
<a class="nav-link text-dark" asp-area="" asp-page="/fancypants">Fancy Pants</a>
Here I'm asking for the page. That asp-page attribute points to a logical page, not a physical file.
在这里,我要页面。 该asp-page属性指向逻辑页面,而不是物理文件。
We can make an IOutboundParameterTransformer that changes URLs to a format (for example) like a WordPress standard slug in the two-words format.
我们可以制作一个IOutboundParameterTransformer,将URL更改为双字格式的格式(例如WordPress标准)。
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
// Slugify value
return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
}
}
Then you let the ASP.NET Pipeline know about this transformer, either in Razor Pages...
然后,您可以在Razor Pages中让ASP.NET Pipeline知道此转换器。
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(options =>
{
options.Conventions.Add(
new PageRouteTransformerConvention(
new SlugifyParameterTransformer()));
});
or in ASP.NET MVC:
或在ASP.NET MVC中:
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
});
Now when I run my application, I get my routing both coming in (from the client web browser) and going out (generated via Razor pages. Here I'm hovering over the "Fancy Pants" link at the top of the page. Notice that it's generated /fancy-pants as the URL.
现在,当我运行应用程序时,我的路由既进入(从客户端Web浏览器)进入,又进入(通过Razor页面生成)路由。在这里,我将鼠标悬停在页面顶部的“ Fant Pants”链接上。它以/ fancy-pants作为URL生成。
So that same code from above that generates anchor tags <a href= gives me the expected new style of URL, and I only need to change it in one location.
因此,从上方生成锚标记<a href =的同一代码为我提供了预期的URL新样式,而我只需要在一个位置进行更改即可。
There is also a new service called LinkGenerator that's a singleton you can call outside the context of an HTTP call (without an HttpContext) in order to generate a URL string.
还有一个名为LinkGenerator的新服务,该服务是一个单例,您可以在HTTP调用的上下文之外(没有HttpContext)调用它,以生成URL字符串。
return _linkGenerator.GetPathByAction(
httpContext,
controller: "Home",
action: "Index",
values: new { id=42 });
This can be useful if you are generating URLs outside of Razor or in some Middleware. There's a lot more little subtle improvements in ASP.NET Core 2.2, but this was the one that I will find the most useful in the near term.
如果您在Razor之外或某些中间件中生成URL,这将很有用。 在ASP.NET Core 2.2中,还有许多细微的改进,但这是我认为在短期内最有用的改进。
Sponsor: Check out the latest JetBrains Rider with built-in spell checking, enhanced debugger, Docker support, full C# 7.3 support, publishing to IIS and more advanced Unity support.
赞助商:使用内置的拼写检查,增强的调试器,Docker支持,完整的C#7.3支持,发布到IIS和更高级的Unity支持,查看最新的JetBrains Rider 。