如何将ASP.NET Core 6 MVC路由系统挂接以自定义URL生成
在旧的.NET框架MVC实现中,我自己创建了路线,以便我也可以影响URL的生成。代码的一部分:
public class RouteBase : Route
{
public RouteBase(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
if (Url.Contains("{segment}") && !values.ContainsKey("segment"))
values["segment"] = requestContext.HttpContext.Items["segmentValue"];
return base.GetVirtualPath(requestContext, values);
}
}
感谢GetVirtualPath,我能够在路由模板中检测一个特定段,并在路由值字典中注入适当的值,以便客户端应用程序在拨打实例url.routeurl时不必指定它( Rutename)。
在ASP.NET Core 6中,我现在使用基于属性的路由,我不知道如何将其连接到此过程中,以便在生成URL时可以将一些值注入路由值字典中。如果我有一个类似的路由模板:
[Route("{segment}/test", Name = "name"]
当我调用此功能时,我希望从代码中的其他某个地方进行注入机制,以便将已知段值注入用于构建URL的路由值:
var url = Url.RouteUrl("name"); // Not passing new { segment = value } as second param
为了进行信息,我只需使用此信息在启动中:
app.MapControllers();
In the old .Net Framework MVC implementations, I was creating routes by myself so that I could also influence urls generation. Part of the code:
public class RouteBase : Route
{
public RouteBase(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
if (Url.Contains("{segment}") && !values.ContainsKey("segment"))
values["segment"] = requestContext.HttpContext.Items["segmentValue"];
return base.GetVirtualPath(requestContext, values);
}
}
Thanks to GetVirtualPath, I was able to detect a particular segment in the route template and inject a proper value in the route values dictionary so that the client app did not have to specify it when calling for instance Url.RouteUrl(routeName).
In asp.net core 6, I'm now using attributes based routing and I don't know how to hook into this so that I can inject some value into the route values dictionary when I generate urls. If I have a route template like so:
[Route("{segment}/test", Name = "name"]
When I call this, I want an injection mechanism from somewhere else in the code so that the known segment value is injected into the route values used to build the url:
var url = Url.RouteUrl("name"); // Not passing new { segment = value } as second param
For information, I simply use this in Startup:
app.MapControllers();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阿米尔(Amir)的回答使我有望找到解决方案(为他赏金奖)。创建自定义Urlhelper是必经之需的方法,但不是urlhelper派生的类。对于Enpoint路由,该框架使用密封 endPointRoutingurlhelper 类。因此,我只需要源自Urlhelperbase,从EndPointRoutingurlhelper粘贴代码,然后添加我的自定义。我很幸运,那里没有内部代码...
这是解决方案。请注意:
Amir's answer put me on track to find a solution (bounty award for him). Creating a custom UrlHelper was the way to go, but not with a UrlHelper derived class. For enpoint routing, the framework is using the sealed EndpointRoutingUrlHelper class. So I just needed to derive from UrlHelperBase, paste the code from EndpointRoutingUrlHelper and add my customizations. I was lucky that there were no internal pieces of code in there...
Here is the solution. Note that:
您可以创建并注册自定义URLHELPER。它将使您能够按照用例操纵行为:
在您的程序中:
然后通过调用url.routeurl(“名称”),将调用您的CustomUrlhelper。
You can create and register a custom UrlHelper. It will give you the ability to manipulate the behavior as per your use case:
and in your Program.cs:
Then by calling the Url.RouteUrl("name"), your CustomUrlHelper will be called.
在ASP.NET Core中,我使用以下两种方法,并且能够成功生成URL。
In Asp.Net Core, I use the below two methods and it is able to successfully generate the URL.