Get Absolute URL from Relative path (refactored method) - c#

I am really surprised that there is no native .NET method to get an absolute url from a relative url. I know this has been discussed many times, but never have come across a satisfactory method that handles this well. Can you help fine tune the method below?
I think all I need left is to auto choose the protocol instead of hard coding it (http/https). Anything else I am missing (caveats, performance, etc)?
public static string GetAbsoluteUrl(string url)
{
//VALIDATE INPUT FOR ALREADY ABSOLUTE URL
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return url;
}
//GET PAGE REFERENCE FOR CONTEXT PROCESSING
Page page = HttpContext.Current.Handler as Page;
//RESOLVE PATH FOR APPLICATION BEFORE PROCESSING
if (url.StartsWith("~/"))
{
url = page.ResolveUrl(url);
}
//BUILD AND RETURN ABSOLUTE URL
return "http://" + page.Request.ServerVariables["SERVER_NAME"] + "/"
+ url.TrimStart('/');
}

This has always been my approach to this little nuisance. Note the use of VirtualPathUtility.ToAbsolute(relativeUrl) allows the method to be declared as an extension in a static class.
/// <summary>
/// Converts the provided app-relative path into an absolute Url containing the
/// full host name
/// </summary>
/// <param name="relativeUrl">App-Relative path</param>
/// <returns>Provided relativeUrl parameter as fully qualified Url</returns>
/// <example>~/path/to/foo to http://www.web.com/path/to/foo</example>
public static string ToAbsoluteUrl(this string relativeUrl) {
if (string.IsNullOrEmpty(relativeUrl))
return relativeUrl;
if (HttpContext.Current == null)
return relativeUrl;
if (relativeUrl.StartsWith("/"))
relativeUrl = relativeUrl.Insert(0, "~");
if (!relativeUrl.StartsWith("~/"))
relativeUrl = relativeUrl.Insert(0, "~/");
var url = HttpContext.Current.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
return String.Format("{0}://{1}{2}{3}",
url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
}

new System.Uri(Page.Request.Url, "/myRelativeUrl.aspx").AbsoluteUri

This one works for me...
new System.Uri(Page.Request.Url, ResolveClientUrl("~/mypage.aspx")).AbsoluteUri

With ASP.NET, you need to consider the reference point for a "relative URL" - is it relative to the page request, a user control, or if it is "relative" simply by virtue of using "~/"?
The Uri class contains a simple way to convert a relative URL to an absolute URL (given an absolute URL as the reference point for the relative URL):
var uri = new Uri(absoluteUrl, relativeUrl);
If relativeUrl is in fact an abolute URL, then the absoluteUrl is ignored.
The only question then remains what the reference point is, and whether "~/" URLs are allowed (the Uri constructor does not translate these).

Here is my own version that handles many validations and relative pathing from user's current location option. Feel free to refactor from here :)
/// <summary>
/// Converts the provided app-relative path into an absolute Url containing
/// the full host name
/// </summary>
/// <param name="relativeUrl">App-Relative path</param>
/// <returns>Provided relativeUrl parameter as fully qualified Url</returns>
/// <example>~/path/to/foo to http://www.web.com/path/to/foo</example>
public static string GetAbsoluteUrl(string relativeUrl)
{
//VALIDATE INPUT
if (String.IsNullOrEmpty(relativeUrl))
return String.Empty;
//VALIDATE INPUT FOR ALREADY ABSOLUTE URL
if (relativeUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| relativeUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
return relativeUrl;
//VALIDATE CONTEXT
if (HttpContext.Current == null)
return relativeUrl;
//GET CONTEXT OF CURRENT USER
HttpContext context = HttpContext.Current;
//FIX ROOT PATH TO APP ROOT PATH
if (relativeUrl.StartsWith("/"))
relativeUrl = relativeUrl.Insert(0, "~");
//GET RELATIVE PATH
Page page = context.Handler as Page;
if (page != null)
{
//USE PAGE IN CASE RELATIVE TO USER'S CURRENT LOCATION IS NEEDED
relativeUrl = page.ResolveUrl(relativeUrl);
}
else //OTHERWISE ASSUME WE WANT ROOT PATH
{
//PREPARE TO USE IN VIRTUAL PATH UTILITY
if (!relativeUrl.StartsWith("~/"))
relativeUrl = relativeUrl.Insert(0, "~/");
relativeUrl = VirtualPathUtility.ToAbsolute(relativeUrl);
}
var url = context.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
//BUILD AND RETURN ABSOLUTE URL
return String.Format("{0}://{1}{2}{3}",
url.Scheme, url.Host, port, relativeUrl);
}

If you're in the context of an MVC Controller or View you can use the UrlHelper which should be accessible via just Url
Url.Content("~/content/images/myimage.jpg")
Which will be fully expanded to /virtual_directoryname/content/images/myimage.jpg
This can be used in a controller or .cshtml file
Yes it is a little odd that it's called Content but it's meant to be used to get an absolute path to a resource so it makes sense

Still nothing good enough using native stuff. Here is what I ended up with:
public static string GetAbsoluteUrl(string url)
{
//VALIDATE INPUT
if (String.IsNullOrEmpty(url))
{
return String.Empty;
}
//VALIDATE INPUT FOR ALREADY ABSOLUTE URL
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return url;
}
//GET CONTEXT OF CURRENT USER
HttpContext context = HttpContext.Current;
//RESOLVE PATH FOR APPLICATION BEFORE PROCESSING
if (url.StartsWith("~/"))
{
url = (context.Handler as Page).ResolveUrl(url);
}
//BUILD AND RETURN ABSOLUTE URL
string port = (context.Request.Url.Port != 80 && context.Request.Url.Port != 443) ? ":" + context.Request.Url.Port : String.Empty;
return context.Request.Url.Scheme + Uri.SchemeDelimiter + context.Request.Url.Host + port + "/" + url.TrimStart('/');
}

When you want to generate URL from your Business Logic layer, you do not have the flexibility of using ASP.NET Web Form's Page class/ Control's ResolveUrl(..) etc. Moreover, you may need to generate URL from ASP.NET MVC controller too where you not only miss the Web Form's ResolveUrl(..) method, but also you cannot get the Url.Action(..) even though Url.Action takes only Controller name and Action name, not the relative url.
I tried using
var uri = new Uri(absoluteUrl, relativeUrl)
approach, but there is a problem too. If the web application is hosted in IIS virtual directory, where the url of the app is 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, in order to overcome such problems, I have written a UrlUtils class which can work from a class library. So, it wont depend on Page class but it depends on ASP.NET MVC. So, if you dont mind adding reference to MVC dll to your class library project then my class will work smoothly. I have tested in IIS virtual directory scenario where the web application url is like this : http://localhost/MyWebApplication/MyPage. I realized that, sometimes we need to make sure that the Absolute url is SSL url or non SSL url. So, I wrote my class library supporting this option. I have restricted this class library so that the relative url can be 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 is : http://localhost/Home/About
Returns : http://localhost/MyWebApplication/Contact
when the page url is : http://localhost/MyWebApplication/Home/About
string absoluteUrl = UrlUtils.MapUrl("~/Contact", UrlUtils.UrlMapOptions.AlwaysSSL);
Returns : **https**://localhost/MyWebApplication/Contact
when the page url is : 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;
}
}

The final version taking care of all previous complaints (ports, logical url, relative url, existing absolute url...etc.) considering the current handler is the page:
public static string ConvertToAbsoluteUrl(string url)
{
if (!IsAbsoluteUrl(url))
{
if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Handler is System.Web.UI.Page)
{
var originalUrl = HttpContext.Current.Request.Url;
return string.Format("{0}://{1}{2}{3}", originalUrl.Scheme, originalUrl.Host, !originalUrl.IsDefaultPort ? (":" + originalUrl.Port) : string.Empty, ((System.Web.UI.Page)HttpContext.Current.Handler).ResolveUrl(url));
}
throw new Exception("Invalid context!");
}
else
return url;
}
private static bool IsAbsoluteUrl(string url)
{
Uri result;
return Uri.TryCreate(url, UriKind.Absolute, out result);
}

check the following code to retrieve absolute Url :
Page.Request.Url.AbsoluteUri
I hope to be useful.

This works fine too:
HttpContext.Current.Server.MapPath(relativePath)
Where relative path is something like "~/foo/file.jpg"

Related

How to redirect unknown routes to 404 page in .NET Razor Pages?

In my application I have 3 group of pages with 3 route patterns.
Pages with one url part after path like:
{domain}/en/about-us/
There is no route parameter on top of these pages right after #page.
Pages with two url parts after path like:
{domain}/en/product/{permalink}/
There is an id as a route parameter on top of this page right after #page.
#page "{id}/"
Pages with 3 url parts after path like:
{domain}/en/{category-permalink1}/{subcategory-permalink2}/{subsubcategory-permalink3}/
Parameters on top of this page are like below:
#page "/{maincat}/{subcat?}/{subsubcat?}/"
Everything is ok but when another part added to URL, it won't redirects to not-found page.
The question is where I can handle this redirection?
As I tried to do:
public class RouteValueRequestCultureProvider : IRequestCultureProvider
{
private readonly CultureInfo[] _cultures;
public RouteValueRequestCultureProvider(CultureInfo[] cultures)
{
_cultures = cultures;
}
/// <summary>
/// get {culture} route value from path string,
/// </summary>
/// <param name="httpContext"></param>
/// <returns>ProviderCultureResult depends on path {culture} route parameter, or default culture</returns>
public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
var defaultCulture = "en";
var path = httpContext.Request.Path;
var routeValues = httpContext.Request.Path.Value.Split('/');
var pathParts = routeValues.Where(p => !string.IsNullOrEmpty(p)).ToArray();
if (pathParts.Length > 4)
{
httpContext.Response.StatusCode = 404;
httpContext.Response.Redirect("/en/not-found/", true);
}
return Task.FromResult(new ProviderCultureResult(routeValues[1]));
}
}
But for URLs more than 4 parts like:
{domain}/{language_code}/{part2}/{part3}/{part4}/{part5}
Redirects to page with 404 status page that is not application's handled 404 page.
Here is an answer for similar issue: Link
app.Use(async (ctx, next) =>
{
await next();
if(ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted)
{
//Re-execute the request so the user gets the error page
string originalPath = ctx.Request.Path.Value;
ctx.Items["originalPath"] = originalPath;
ctx.Request.Path = "/error/404";
await next();
}
});
Make sure to add this before app.UseEndpoints as mentioned in the original answer.

RedirectPermanent not redirecting to the url

I'm coding a simple URL shortener.
Everything is working, except the redirection.
Here is the code that tries to redirect:
public async Task<ActionResult> Click(string segment)
{
string referer = Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : string.Empty;
Stat stat = await this._urlManager.Click(segment, referer, Request.UserHostAddress);
return this.RedirectPermanent(stat.ShortUrl.LongUrl);
}
When I input a link that is shortened, like this http://localhost:41343/5d8a2a, it redirects me to http://localhost:41343/www.google.com.br instead of www.google.com.br.
EDIT
After checking the answer, it works. Here is the final snippet of code.
if (!stat.ShortUrl.LongUrl.StartsWith("http://") && !stat.ShortUrl.LongUrl.StartsWith("https://"))
return this.RedirectPermanent("http://" + stat.ShortUrl.LongUrl);
else
return this.RedirectPermanent(stat.ShortUrl.LongUrl);
Thanks!
Instead of RedirectPermanent() try using Redirect() like below. The specified URL has to be a absolute URL else it will try to redirect to within your application.
You can check for existence of http:// and add it accordingly
if(!stat.ShortUrl.LongUrl.Contains("http://"))
return Redirect("http://" + stat.ShortUrl.LongUrl);
(OR)
Use StartsWith() string function
if(!stat.ShortUrl.LongUrl.StartsWith()("http://"))
return Redirect("http://" + stat.ShortUrl.LongUrl);

C# Uri with username as "domainname\username"

I have a string like
<Scheme>:\\<domain>\<domainuser>:<EncryptedPassword>#servername\
I want to be able to create Uniform resource identifier (URI) and easy access the parts of the URI. For most part using C# Uri class works great. But I get invalid URI exceptions when user is provided as "domainname\domainuser".
How best to handle this in C#.
You must PercentEncode(/EscapeDataString) such strings.
Like in question above username "xyz_domain\abc_user" must be encoded to "xyz_domain%5Cabc_user" to before creating URI object.
Later after extracting you can do PercentDecode(/UnescapeDataString) of the string using UnescapeDataString method from Uri Class.You can use UnescapeDataString Method from C# Uri Class
Here is the code
public static string GetUsername(this Uri uri)
{
if (uri == null || string.IsNullOrWhiteSpace(uri.UserInfo))
return string.Empty;
var items = uri.UserInfo.Split(new[] { ':' });
//Replace precent encoding in the username.
var result = Uri.UnescapeDataString(items[0]);
return result.Length > 0 ? result : string.Empty;
}
Similar scheme can be applied to any part of the uri string. Just remember to UnescapeDataString the EscapeDataStrings.

Calling node.NiceUrl gives me # in Umbraco

Doing a project in Umbraco, and i've encountered problems in one case that when calling node.NiceUrl I get # as the result. What is weird though is that if i debug it somehow it resolves into the correct url.
var pages = Pages.Select((item, index) => new
{
Url = item.NiceUrl,
Selected = item.Id == currentPage.Id,
Index = index
}).ToList();
Where Pages is obtained from:
CurrentPage.Parent.ChildrenAsList
If I do it this way, it works, but I don't know why.
Url = new Node(item.Id).NiceUrl,
I've encountered this error and it was because the id belonged to a media node.
Media is treated differently to other content and there's no easy way of getting the url because different types of media store the url in different ways depending on context. That's why the NiceUrl function doesn't work for media (according to the umbraco developers).
My specific scenario was using images that had been selected using a media picker. I got the url via the following code. I wrapped it up in an extension method so you can consume it from a template in a convenient way.
public static string GetMediaPropertyUrl(this IPublishedContent thisContent, string alias, UmbracoHelper umbracoHelper = null)
{
string url = "";
if (umbracoHelper == null)
umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var property = thisContent.GetProperty(alias);
string nodeID = property != null ? property.Value.ToString() : "";
if (!string.IsNullOrWhiteSpace(nodeID))
{
//get the media via the umbraco helper
var media = umbracoHelper.TypedMedia(nodeID);
//if we got the media, return the url property
if (media != null)
url = media.Url;
}
return url;
}
Try like this
Url = umbraco.library.NiceUrl(Item.Id);

C#, Is there a better way to verify URL formatting than IsWellFormedUriString?

Is there a better/more accurate/stricter method/way to find out if a URL is properly formatted?
Using:
bool IsGoodUrl = Uri.IsWellFormedUriString(url, UriKind.Absolute);
Doesn't catch everything. If I type htttp://www.google.com and run that filter, it passes. Then I get a NotSupportedExceptionlater when calling WebRequest.Create.
This bad url will also make it past the following code (which is the only other filter I could find):
Uri nUrl = null;
if (Uri.TryCreate(url, UriKind.Absolute, out nUrl))
{
url = nUrl.ToString();
}
The reason Uri.IsWellFormedUriString("htttp://www.google.com", UriKind.Absolute) returns true is because it is in a form that could be a valid Uri. URI and URL are not the same.
See: What's the difference between a URI and a URL?
In your case, I would check that new Uri("htttp://www.google.com").Scheme was equal to http or https.
Technically, htttp://www.google.com is a properly formatted URL, according the URL specification. The NotSupportedException was thrown because htttp isn't a registered scheme. If it was a poorly-formatted URL, you would have gotten a UriFormatException. If you just care about HTTP(S) URLs, then just check the scheme as well.
#Greg's solution is correct. However you can steel using URI and validate all protocols (scheme) that you want as valid.
public static bool Url(string p_strValue)
{
if (Uri.IsWellFormedUriString(p_strValue, UriKind.RelativeOrAbsolute))
{
Uri l_strUri = new Uri(p_strValue);
return (l_strUri.Scheme == Uri.UriSchemeHttp || l_strUri.Scheme == Uri.UriSchemeHttps);
}
else
{
return false;
}
}
This Code works fine for me to check a Textbox have valid URL format
if((!string.IsNullOrEmpty(TXBProductionURL.Text)) && (Uri.IsWellFormedUriString(TXBProductionURL.Text, UriKind.Absolute)))
{
// assign as valid URL
isValidProductionURL = true;
}

Categories

Resources