If you want to generate a URL from the level of your business logic, you do not have the flexibility to use the ASP.NET Web Form / Control ResolveUrl (..) page class, etc. In addition, you may need to generate the URL from the ASP.NET MVC controller too, where you not only skip the Web Form ResolveUrl (..) method, but also cannot get Url.Action (..), although Url.Action accepts only the controller name and action name, not the relative URL.
I tried to use
var uri = new Uri (absoluteUrl, relativeUrl)
but there is a problem. If the web application is located in the IIS virtual directory, where the application URL looks like this: http://localhost/MyWebApplication1/ and the relative URL is "/ myPage", then the relative URL is resolved as " http://localhost/MyPage ", which is another problem.
Therefore, to overcome such problems, I wrote the UrlUtils class, which can work from the class library. Thus, it does not depend on the page class, but depends on ASP.NET MVC . So, if you do not mind adding a link to the MVC dll to your class library project, then my class will work smoothly. I tested in an IIS virtual directory script where the web application URL looks like this: http://localhost/MyWebApplication/MyPage . I realized that sometimes we need to make sure that the Absolute URL is an SSL URL or not an SSL url. So, I wrote my class library that supports this option. I have limited this class library so that the relative URL can be an absolute URL or a relative URL that starts with '~ /'.
Using this library, I can call
string absoluteUrl = UrlUtils.MapUrl("~/Contact");
Returns: http://localhost/Contact when the page URL: http://localhost/Home/About
Returns: http://localhost/MyWebApplication/Contact when the page URL: http://localhost/MyWebApplication/Home/About
string absoluteUrl = UrlUtils.MapUrl("~/Contact", UrlUtils.UrlMapOptions.AlwaysSSL);
Returns: **https**://localhost/MyWebApplication/Contact when the page URL: http://localhost/MyWebApplication/Home/About
Here is my class library:
public class UrlUtils { public enum UrlMapOptions { AlwaysNonSSL, AlwaysSSL, BasedOnCurrentScheme } public static string MapUrl(string relativeUrl, UrlMapOptions option = UrlMapOptions.BasedOnCurrentScheme) { if (relativeUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || relativeUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) return relativeUrl; if (!relativeUrl.StartsWith("~/")) throw new Exception("The relative url must start with ~/"); UrlHelper theHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); string theAbsoluteUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + theHelper.Content(relativeUrl); switch (option) { case UrlMapOptions.AlwaysNonSSL: { return theAbsoluteUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? string.Format("http://{0}", theAbsoluteUrl.Remove(0, 8)) : theAbsoluteUrl; } case UrlMapOptions.AlwaysSSL: { return theAbsoluteUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? theAbsoluteUrl : string.Format("https://{0}", theAbsoluteUrl.Remove(0, 7)); } } return theAbsoluteUrl; } }