7

I need to construct the URL of a page in a String, to send it an email (as part of an email verification system). If i use the ~ symbol to denote the app root, it is taken literally.

The app will be deployed on a server on three different sites (on different ports) and each site can be accessed via 2 different URLs (one for LAn and one for internet).

So hardcoding the URL is out of question. I want to construct the url to verify.aspx in my application

Please help

2
  • Are you going to be sending out a different URL if they are inside the LAN, or do you want it to be the same URL? Commented Apr 29, 2009 at 12:04
  • It would help to see the actual code how you're attempting to construct that string. Commented Oct 14, 2011 at 23:19

7 Answers 7

13

You need this:

HttpContext.Current.Request.ApplicationPath

It's equivalent to "~" in a URL.

http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

ApplicationPath doesn't produce a URL that is usable in an email. For an email, you would need the full URL, complete with "http://.../" etc. ApplicationPath would only give you "/".
13

Unfortunately none of the methods listed generated the full url starting from http://---.

So i had to extract these from request.url. Something like this

Uri url=HttpContext.Current.Request.Url;
StringBuilder urlString = new StringBuilder();
urlString.Append(url.Scheme);
urlString.Append("://");
urlString.Append(url.Authority);
urlString.Append("/MyDesiredPath");

Can someone spot any potential problems with this?

Comments

3

Try:

HttpRequest req = HttpContext.Current.Request;
string url = req.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped)
    + ((req.ApplicationPath.Length > 1) ? req.ApplicationPath : "");

Comments

2

You need to put the URL as part of your web application's configuration. The web application does not know how it can be reached from the outside world.

E.g. consider a scenario where there's multiple proxies and load balancers in front of your web server... how would the web server know anything but its own IP?

So, you need to configure each instance of your web application by adding the base URL e.g. as an app setting in its web.config.

Comments

0

You can use HttpRequest.RawURL (docs here)property and base your URL on that, but if you are behind any kind of redirection, the RawURL may not reflect the actual URL of your application.

Comments

0

I ended up with this. I take the request url, and use the position of Request.ApplicationRoot to discover the left part of the uri. Should work with applications hosted in a virtual directory "/example" or in the root "/".

    private string GetFullUrl(string relativeUrl)
    {
        if (string.IsNullOrWhiteSpace(relativeUrl)) 
            throw new ArgumentNullException("relativeUrl");

        if (!relativeUrl.StartsWith("/"))
            throw new ArgumentException("url should start with /", "relativeUrl");

        string current = Request.Url.ToString();
        string applicationPath = Request.ApplicationPath;
        int applicationPathIndex = current.IndexOf(applicationPath, 10, StringComparison.InvariantCultureIgnoreCase);

        // should not be possible
        if (applicationPathIndex == -1) throw new InvalidOperationException("Unable to derive root path");

        string basePath = current.Substring(0, applicationPathIndex);
        string fullRoot = string.Concat(
            basePath,
            (applicationPath == "/") ? string.Empty : applicationPath,
            relativeUrl);

        return fullRoot;
    }

Comments

0

This has always worked for me:

string root = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "");

2 Comments

Please see how to answer questions on SO.
I didn't understand the downvote. Anything wrong with the suggested solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.