ProfK
ProfK

Reputation: 51063

How to build an absolute URL for the host currently in use in an ASP.NET app

I am currently in a dev only phase of development, and am using the VS built-in web server, configured for a fixed port number. I am using the following code, in my MembershipService class, to build an email body with a confirmation link, but obviously this must change when I deploy to our prod host.

var url = string.Format("http://localhost:59927/Account/CompleteRegistration/{0}", newMember.PendingReplyId);

How can I build this URL to always reflect the host that the code is running on, e.g. when deployed to prod the URL should be http://our-live-domain.com/Account/..etc.

MORE INFO: This URL will is included in an email to a new user busy registering an account, so I cannot use a relative URL.

Upvotes: 2

Views: 3404

Answers (4)

Ekus
Ekus

Reputation: 1880

    new Uri(
        Request.Url, // base URI from current context
        "/Account/CompleteRegistration/1234" // address relative to the base URI, use / if needed
    ).ToString();

This results in http://your.server/Account/CompleteRegistration/1234 .

It works great for relative links, even if our current location is not root:

    new Uri(
        Request.Url, // we are at http://server/app/subfolder/page.aspx?q=1
        "page2.aspx"
    ).ToString(); //produces http://server/app/subfolder/page2.aspx

BTW, since it's of System.Uri type (unlike Request.RawUrl which is a relative path string), it has tons of useful properties, but typically you will just use .ToString().

Although you can't use ~ (tilde) paths directly, it's very simple to resolve them, and you should do it when in doubt:

    new Uri(
        Request.Url, 
        Page.ResolveUrl("~/folder/test") // use this! tilde is your friend!!!
        ).ToString(); // this will always point to our app even if it's in a virtual folder instead of root

Upvotes: 0

citronas
citronas

Reputation: 19365

Have a setting for this in your web.config

Like this:

<appSettings>
    <add key="BaseURL" value="http://localhost:59927/"/>
</appSettings>

Access the value from the code. If you store multiple values in the appSettings and use them all over your project, I'd avise to use a wrapper class.

public class AppSettingsWrapper
{
        public static String BaseURL
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["BaseURL"].ToString(); }
        }
         // you can also insert other values here, that need to be cast into a specific datatype
        public static int DefaultPageID
        {
            get { return int.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageID"].ToString()); }
        }
}

You can assemble your string like this:

String url = string.Format("{0}{1}", AppSettingsWrapper.BaseURL, ResolveUrl(String.Format("~/Account/CompleteRegistration/{0}", newMember.PendingReplyId)));

Upon deployment, you need to replace the settings from the appSettings section. You can do this by using web config transforms. Have a look at this article http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms-to-replace-appsettings-and-connectionstrings, which shows you how to this. You would create solution configurations for your testserver and your production server

Upvotes: 1

Jonathan Stanton
Jonathan Stanton

Reputation: 2620

While you can always set the host name and port as a setting which can then be read at run time (Very useful if the machine you have has multiple host headers, which might be in the case of load balancing). You can work out the Url from the following components :

Request["SCRIPT_NAME"] eg "/default.aspx"
Request["SERVER_NAME"] eg "localhost"
Request["SERVER_PORT"] eg "80"

Hope that this helps.

Jonathan

Upvotes: 0

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

use appSettings section in web.conf it will allow you to configure setting for production server.

and use ConfigurationManager class for acces to appSetting section.

Upvotes: 0

Related Questions