I want to get the absolute root Url of an ASP.NET application dynamically. This needs to b开发者_如何学编程e the full root url to the application in the form: http(s)://hostname(:port)/
I have been using this static method:
public static string GetSiteRootUrl()
{
string protocol;
if (HttpContext.Current.Request.IsSecureConnection)
protocol = "https";
else
protocol = "http";
StringBuilder uri = new StringBuilder(protocol + "://");
string hostname = HttpContext.Current.Request.Url.Host;
uri.Append(hostname);
int port = HttpContext.Current.Request.Url.Port;
if (port != 80 && port != 443)
{
uri.Append(":");
uri.Append(port.ToString());
}
return uri.ToString();
}
BUT, what if I don't have HttpContext.Current
in scope?
I have encountered this situation in a CacheItemRemovedCallback
.
For WebForms, this code will return the absolute path of the application root, regardless of how nested the application may be:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/")
The first part of the above returns the scheme and domain name of the application (http://localhost
) without a trailing slash. The ResolveUrl
code returns a relative path to the application root (/MyApplicationRoot/
). By combining them together, you get the absolute path of the web forms application.
Using MVC:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~/")
or, if you are trying to use it directly in a Razor view:
@HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/")
You might try getting the raw URL and trimming off everything after the path forward slash. You could also incorporate ResolveUrl("~/")
.
public static string GetAppUrl()
{
// This code is tested to work on all environments
var oRequest = System.Web.HttpContext.Current.Request;
return oRequest.Url.GetLeftPart(System.UriPartial.Authority)
+ System.Web.VirtualPathUtility.ToAbsolute("~/");
}
public static string GetFullRootUrl()
{
HttpRequest request = HttpContext.Current.Request;
return request.Url.AbsoluteUri.Replace(request.Url.AbsolutePath, String.Empty);
}
I've solved this by adding a web.config setting in AppSettings ("SiteRootUrl"). Simple and effective, but yet another config setting to maintain.
UrlHelper url = new UrlHelper(filterContext.RequestContext);
string helpurl = url.Action("LogOn", "Account", new { area = "" },
url.RequestContext.HttpContext.Request.Url.Scheme);
Can get you the absolute url
@saluce had an excellent idea, but his code still requires an object reference and therefore can't run in some blocks of code. With the following, as long as you have a Current.Request
the following will work:
With HttpContext.Current.Request
Return .Url.GetLeftPart(UriPartial.Authority) + .ApplicationPath + If(.ApplicationPath = "/", Nothing, "/")
End With
This will work no matter the protocol, port, or root folder.
This has always worked for me
string root = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "");
Based off Uri's but stripping query strings and handling when it is a virtual directory off IIS:
private static string GetSiteRoot()
{
string siteRoot = null;
if (HttpContext.Current != null)
{
var request = HttpContext.Current.Request;
siteRoot = request.Url.AbsoluteUri
.Replace(request.Url.AbsolutePath, String.Empty) // trim the current page off
.Replace(request.Url.Query, string.Empty); // trim the query string off
if (request.Url.Segments.Length == 4)
{
// If hosted in a virtual directory, restore that segment
siteRoot += "/" + request.Url.Segments[1];
}
if (!siteRoot.EndsWith("/"))
{
siteRoot += "/";
}
}
return siteRoot;
}
精彩评论