36

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?

pgb
  • 24,813
  • 12
  • 83
  • 113
user115195
  • 363
  • 1
  • 3
  • 4

6 Answers6

77

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
  • 414,610
  • 91
  • 852
  • 789
  • 2
    And if the application is a virtual directory in IIS, but the page we're requested is inside several folder, but we want path including virtual directory but not folders, how to do this? – Krunal Oct 17 '13 at 09:31
  • 1
    @Krunal [HttpRuntime.AppDomainAppVirtualPath](https://msdn.microsoft.com/en-us/library/system.web.httpruntime.appdomainappvirtualpath(v=vs.110).aspx) or Request.ApplicationPath – jproch Nov 11 '16 at 15:48
18
Request.Url

it contains several points that you might consider to use, see the image below:

enter image description here

balexandre
  • 73,608
  • 45
  • 233
  • 342
7

In .NET 4.5

    VirtualPathUtility.ToAppRelative(path)
Adamy
  • 2,789
  • 3
  • 27
  • 25
6

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;

Taryn
  • 242,637
  • 56
  • 362
  • 405
Rhonald Moses
  • 61
  • 1
  • 1
2

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
  • 22,920
  • 8
  • 63
  • 107
0

Url.Content("~") worked great for me and is nice and simple. I used it in the view like this:

<a href="@(Url.Content("~" + attachment))">

Here my attachment is a path like "/Content/Documents/Blah.PDF".

When my app is published to a IIS site that uses a virtual directory, Url.Content("~") resolves to just the virtual directory name like, "/app-test", for example.

Jess
  • 23,901
  • 21
  • 124
  • 145