views:

2924

answers:

7

How do I know the the complete virtual path that my application is currently hosted? For example:

http://www.mysite.com/myApp

or

http://www.mysite.com/myApp/mySubApp

I know the application path of HttpRequest but it only returns the folder name that my application is currently hosted, but how do I get the initial part?

A: 

Try this (Haven't tried it)

public string GetVirtualPath(string physicalPath)
{
string rootpath = Server.MapPath("~/");
physicalPath = physicalPath.Replace(rootpath, "");
physicalPath = physicalPath.Replace("\\", "/");
return "~/" + physicalPath;
}

Link 1

Link 2

Shoban
A: 

To fully answer this question I think you need to describe what your actually trying to do and what is making your path value "unknown"

Usually when I'm running into Path finding issues this blog post by Rick Strahl clears things up for me: http://www.west-wind.com/weblog/posts/269.aspx

Might have to mix and match to get what you want.

jfar
+6  A: 

The domain name part of the path is not really a property of the application itself, but depends on the requesting URL. You might be able to reach a single Web site from many different host names. To get the domain name associated with the current request, along with the virtual path of the current application, you could do:

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

Technically, an "application" is a virtual directory defined in IIS and Request.ApplicationPath returns exactly that. If you want to get the folder in which the current request is handled, you can do this:

VirtualPathUtility.GetDirectory(Request.Path)

ASP.NET has no idea how to distinguish your sub-application from a bigger application if it's not defined as a virtual directory in IIS. Without registering in IIS, it just sees the whole thing as a single app.

Mehrdad Afshari
Thanks Mehrdad.
+3  A: 
balexandre
A: 

Hi,

I am using url rewriting and Ajax, now wherever anything related to ajax is done and the page is posted back, the postback url changes(i.e. its adding the directory name in the url) coz of which iam getting the following javascript error and i can't move forward.

Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

I know it is an issue which lot of people are facing but how can i make url rewriting work with ajax..

(The site is not allowing me to post a code snippet)

I have even used Context.Items.Add which will fix my url for the entire post-response cycle. I have also put the RewriteForm code in my master as u did, and have also created a class in my app_code

Any guidance

A: 

The below code will solve the purpose, however you have to do a bit tuning for two types of scenarios:

  1. Hosted as separate web application.
  2. Hosted as Virtual application within a web application.

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;

Enjoy coding.

Rhonald Moses
A: 

Update on my above reply:

the mentioned code snipped works on both scenarios.

Enjoy coding :-)

Rhonald Moses