public class CustomRoute : RouteBase
{
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues)
{
var virtual_path_data = new VirtualPathData( this, "http://example.com" );
return virtual_path_data;
}
}
ASP.NET 似乎自动向从 VirtualPathData 返回的任何(绝对)路径添加前导斜杠,在此示例中返回
/http://example.com
.题:
Url.Action
, RedirectToAction
以及我不知道的其他用例。 背景
我写了一个基于 XML 的路由引擎,支持多个域名。所以当我生成一个只能在另一个域中使用的 URL 时,我想生成一个绝对 URL,包括域名。
<host name="exm.com" controller="ShortUrl">
<parameter name="code" action="Redirect" />
</host>
<host name="*" default="example.com" controller="Home" action="Index">
<x name="demo" controller="Home" action="Demo" />
<x name="comments" controller="Comment" action="List">
<x name="write" action="Write" />
<x name="delete" action="Delete" />
<x>
</host>
产生如下组合:
Url.Action("Demo", "Home")
http://example.com/demo
Url.Action("Write", "Comment")
http://example.com/comments/write
Url.Action("Redirect", "ShortUrl", new { code = "gH8x" } )
http://exm.cm/gH8x
请注意,example.com 和 exm.com 指的是相同的应用程序 IP,并在应用程序内路由。
最佳答案
这是某人的博客文章,似乎对您正在尝试做的事情有说明。 MVC Domain Routing
关于asp.net - 从 RouteBase.GetVirtualPath 返回绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973441/