IRouter接口中的GetVirtualPath方法有什么用
What is the use of the GetVirtualPath method in the IRouter interface
因此,当您编写自己的路由器时,您可以从 IRouter
接口实现。这迫使您实施以下两种方法。
VirtualPathData GetVirtualPath(VirtualPathContext context)
Task RouteAsync(RouteContext routeContext)
我知道 RouteAsync
方法会在每次请求时被调用,并且应该自行处理路由。那么 GetVirtualPath
方法呢?现在我按照这些思路使用它:
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return null;
}
目前效果很好。现在是真正的问题
- 我应该就这样离开吗?
- 它的目的是什么?
- 它应该包含什么代码?
第一个方法 GetVirtualPath 由框架在内部使用,以在类似 HTML.ActionLink 的方法中基于此路由生成 url,第二个方法 RouteAsync 是我们的逻辑真正驻留的地方。
我们的 class 应该以这样的开头:
public class MyRouter : IRouter
{
private readonly IRouter _defaultRouter;
public MyRouter (IRouter defaultRouter)
{
_defaultRouter = defaultRouter;
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return _defaultRouter.GetVirtualPath(context);
}
public async Task RouteAsync(RouteContext context)
{
}
}
来自URL generation with LinkGenerator
URL generation is the process by which routing can create a URL path based on a set of route values. This allows for a logical separation between route handlers and the URLs that access them.
URL generation follows a similar iterative process, but it starts with user or framework code calling into the GetVirtualPath method of the route collection. Each route has its GetVirtualPath method called in sequence until a non-null VirtualPathData is returned.
此外,asp.net core 有 Route class 来实现 IRouter
Routing provides the Route class as the standard implementation of IRouter. Route uses the route template syntax to define patterns to match against the URL path when RouteAsync is called. Route uses the same route template to generate a URL when GetVirtualPath is called.
因此,当您编写自己的路由器时,您可以从 IRouter
接口实现。这迫使您实施以下两种方法。
VirtualPathData GetVirtualPath(VirtualPathContext context)
Task RouteAsync(RouteContext routeContext)
我知道 RouteAsync
方法会在每次请求时被调用,并且应该自行处理路由。那么 GetVirtualPath
方法呢?现在我按照这些思路使用它:
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return null;
}
目前效果很好。现在是真正的问题
- 我应该就这样离开吗?
- 它的目的是什么?
- 它应该包含什么代码?
第一个方法 GetVirtualPath 由框架在内部使用,以在类似 HTML.ActionLink 的方法中基于此路由生成 url,第二个方法 RouteAsync 是我们的逻辑真正驻留的地方。
我们的 class 应该以这样的开头:
public class MyRouter : IRouter
{
private readonly IRouter _defaultRouter;
public MyRouter (IRouter defaultRouter)
{
_defaultRouter = defaultRouter;
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return _defaultRouter.GetVirtualPath(context);
}
public async Task RouteAsync(RouteContext context)
{
}
}
来自URL generation with LinkGenerator
URL generation is the process by which routing can create a URL path based on a set of route values. This allows for a logical separation between route handlers and the URLs that access them.
URL generation follows a similar iterative process, but it starts with user or framework code calling into the GetVirtualPath method of the route collection. Each route has its GetVirtualPath method called in sequence until a non-null VirtualPathData is returned.
此外,asp.net core 有 Route class 来实现 IRouter
Routing provides the Route class as the standard implementation of IRouter. Route uses the route template syntax to define patterns to match against the URL path when RouteAsync is called. Route uses the same route template to generate a URL when GetVirtualPath is called.