What is the best way to determine the url for local / staging / production?

For local testing, the URL looks something like this: http: // localhost: 29234 / default.aspx

To configure, the application is located in the virtual directory: http: //stage/OurApp/default.aspx

For production, this is the root of http://www.ourcompany.com/default.aspx

However, sometimes we need to redirect to a specific directory. We do not always know exactly where we are.

So, how do I redirect to say / subdir 1 / mypage.aspx?

ADDITIONAL INFORMATION

I forgot an important element. This URL is sent back to the browser so that javascript code can do the redirection. (Strange, I know). Therefore, regular ResolveUrl ("~ / pagename.aspx") will not give complete information ...

UPDATE 2 I ended up with the following, which seems to work in all directions ... It looks a little ugly though.

StringBuilder buildUrl = new StringBuilder(@"http://"); buildUrl.Append(Request.Url.Host); if (Request.Url.Port != 80) { buildUrl.Append(":"); buildUrl.Append(Request.Url.Port.ToString()); } buildUrl.Append(this.ResolveUrl("~/Pages/Customers.aspx")); buildUrl.Append(String.Format("?AccountId={0}&tabName=Tab2&primaryCustomerId={1}", acctId, custId)); 
+4
source share
5 answers

When the paths begin to diverge between different environments, and you cannot bring any sanity to the situation, it's time to put puttin 'paths in web.config.

This is not a cure for inconsistent file paths, but it will make your code consistent and you don’t have to worry about letting me know where I am.

+1
source

Tilda is a shortcut for HttpRuntime.AppDomainAppVirtualPath ( more )

 ~/subdir1/mypage.aspx 
0
source

If subdir1 is a directory in your web application, you can use a relative link (subdir1 / mypage.aspx instead of /subdir/mypage.aspx - note the absence of the first slash). This way, it won’t matter where your application is because the links will refer to the current page.

0
source

Suppose you can use the BASE tag for a page that may be the root. using this, all your relative paths will be resolved based on the BASE path.

0
source

General tips

I recommend saving the path in your settings. There are reasons why some of our projects need different paths and URLs, and we cannot always avoid using the tilde (~).

Our strategy

In our projects here, in Inntec, our web.config contains a database connection string and a variable indicating what the environment is - Production, Staging, Development, etc.

Then in the database we have a set of variables for each environment, and there is a class that takes a lot of settings and pulls / caches the correct setting for the current environment. Therefore, in our code we can say: Settings.AppUrl and everything just works.

We use Redgate Sql Data Compare to synchronize settings in all instances (therefore, in each environment there are always settings for all environments), and there are unit tests that ensure that each environment has a full batch of settings.

This is one way to do this ... Until now, it has worked very well for us.

0
source

All Articles