Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get main part of url including virtual directory

I am working with .net 4.0 c#.

I want to be able to get the url from the current http request, including any virtual directory. So for example (request and sought value):

http://www.website.com/shop/test.aspx -> http://www.website.com/shop/

http://www.website.com/test.aspx -> http://www.website.com/

http://website.com/test.aspx -> http://website.com/

How is it possible to achieve this?

like image 956
amateur Avatar asked Nov 03 '11 18:11

amateur


People also ask

How to create virtual directory?

Right-click the Web site that you want (for example, Default Web Site), point to New, and then click Virtual Directory. On the Welcome to the Virtual Directory Creation Wizard page, click Next. On the Virtual Directory Alias page, type the alias that you want (for example, Sales), and then click Next.

Why We create virtual directory?

The virtual directory integrates identity data from multiple heterogeneous data stores and presents it as though it were coming from one source. This ability to reach into disparate repositories makes virtual directory technology ideal for consolidating data stored in a distributed environment.


3 Answers

This is what I use

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
like image 105
Marek Karbarz Avatar answered Oct 13 '22 14:10

Marek Karbarz


Request.Url should contain everything you need. At that point it's a matter of checking the string, and what you prefer to grab from it. I've used AbsoluteUri before, and it works.

This example isn't fool proof, but you should be able to figure out what you need from this:

string Uri = Request.Url.AbsoluteUri;
string Output = Uri.Substring(0, Uri.LastIndexOf('/') + 1 );
like image 4
Doozer Blake Avatar answered Oct 13 '22 16:10

Doozer Blake


This solution could work and is shorter:

string url = (new Uri(Request.Url, ".")).OriginalString;
like image 1
Oscar Avatar answered Oct 13 '22 16:10

Oscar