2

OK most of us develop and test the Asp.NET Web Applications in our localhost before publishing them to the production server. But to me, localhost is a pain sometimes since I cannot get the absolute path properly. Because lets say my app is located in http://localhost/MyApp/ I cannot properly get the host. I can get it with a couple of code but I like to make it more generic so whenever some other developer puts the same application in another location such as http://localhost/TheirApp, then it should work fine.

Sample Problem:

Whenever I use absolute path like this /aboutus.aspx, it causes this http://localhost/aboutus.aspx and omits MyApp. If I use relative path, then it becomes http://localhost/MyApp/IamHere.aspx/aboutus.aspx which is rather disturbing.

So if Request.Url.Authority or Request.Url.Host would return http://localhost/MyApp then we could append our url using these and find a nice solution.

An Alternative Solution:

I could do this:

if(Request.Url.Host.StartsWith("localhost"))
{
    string[] segments = Request.Url.AbsolutePath.Split("/");
    var localHost = Request.Url.Authority + "/" + segments[0];
}

then I can use localHost variable and append my path to it.

But I like to learn how you guys are dealing with such problem?

Thanks,

3 Answers 3

6

I'm not quite sure of what you are trying to achieve, but you can use the tilde (~) to refer to the web application's root and that should solve your problem.

string url = ResolveUrl("~/aboutus.apsx");

The ASP.NET application will figure out by itself if ~ should be localhost, localhost:34534, someserver.net/Deep/Path or anything else.

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

2 Comments

u don't even need the tilde. using bare filename will be current directory of current web app of current host and port. or you can say the same thing more explicitly ./whatever.aspx
Mind if I cried? How come I forgot ~/ all these days :/
1

It's the job of well behaving apps to work on whichever host and whichever path, either through a config parameter or the app figures it out. It shouldn't be too difficult to figure out the path (the bits between) the program/script itself and hostname. In any case for the most part the script/program can use relative paths only, the webserver adds the other bits automatically for you...so no need to write it.

Comments

0

Maybe an alternative could be to create a link or what I would call a 'pseudo path' to the targetted folder. For example you create a link 'TheirApp' that points to the folder 'MyApp' (or the reverse):

TheirApp --> MyApp

MyApp --> TheirApp

Or you could do so in appropriate subfolders:

My/Path/MyApp --> Their/Path/TheirApp

Their/Path/TheirApp --> My/Path/MyApp

Comments

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.