从外部类库中,我想转换一个路径,如下所示:
~/酒吧/雀鳝
到如下路径:
http://foo.com/bar/gar
这是一个ASP。. NET MVC应用程序。
因为我们不在视图中,所以我不能使用外部类库中的UrlHelper
类。这是因为这个类是一个实例类,它没有默认的无参数构造函数。它需要一个System.Web.Routing.RequestContext
的实例。
我尝试的另一个选项是:
var absoluteRedirectUri = System.Web.VirtualPathUtility
.ToAbsolute(settings.Parameters.RedirectUri.Value,
HttpContext.Current.Request.ApplicationPath);
但这仍然产生/bar/gar而不是http://foo.com/bar/gar
你可以这样做:
public string FullyQualifiedApplicationPath
{
get
{
//Return variable declaration
string appPath = null;
//Getting the current context of HTTP request
HttpContext context = HttpContext.Current;
//Checking the current context content
if (context != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
context.Request.Url.Scheme,
context.Request.Url.Host,
context.Request.Url.Port == 80
? string.Empty : ":" + context.Request.Url.Port,
context.Request.ApplicationPath);
}
if (!appPath.EndsWith("/"))
appPath += "/";
return appPath;
}
}
这里是链接,您可以在这里阅读详细信息。