views:

61

answers:

2

I have the following two lines of codes in my model, however, both virtual and path have values "\". Where have I gone wrong?

var virtual = VirtualPathUtility.ToAbsolute(HttpContext.Current.Request.ApplicationPath);

var path =HttpContext.Current.Request.ApplicationPath;

+1  A: 

From MSDN:

Gets the ASP.NET application's virtual application root path on the server.

So this is the part of the URL's path that is the root of the IIS Web Application the code is running in. The root URL ("http://domain/") is always an IIS Application, so will give "/" as its ApplicationPath.

You perhaps need to convert some child (virtual) folder into an IIS Application to see a longer path result.

Updated from comment:

I want to have the part "Http://servername:/..."

This information is all available within the properties of Request.Uri. In particular "http" is Uri.Scheme, severname is Uri.Host and the port is Uri.Port (but check Uri.IsDefaultPort to check if you need to specify it).

Richard
How can I have the path and virtual path?
@user28118: There are multiple means for "path" in this context, which is why there are many path related properties on `HttpRequest`, you might be looking for `PhysicalApplicationPath` or `PhysicalPath`. Take a look at the properties of `HttpContext.Current.Request` in a debugger.
Richard
Can yu please tell me how can I have the path and virtual path using the mvc url builders/resolvers
@user281180: please expand the question with an example, including the path of the application and request, and what you are looking for as a result.
Richard
I would like to have something like this for the virtual path :http://localhost:2226/WebUI/and for the path : C:\Dev\DEV\Test\Code\WebUI\
This is what I had when I was on web asp.net, but now would like to have the path in MVC.
@user281180: As Richard said. HttpContext.Current.Request.PhysicalApplicationPath I think its what you want. _or not_ please explain it
cem
I want to have the part "Http://servername:<port>/..."
The above is not clear enough and my code would not be clean. I would like to generate a link and send it to the users via mail.
@user281180: Encapsulate in a helper and then use that (could be an extension method to System.Uri) -- if everything we needed was part of the framework, then there would be no work (and jobs) for us.
Richard
can you give me an example please, I am new to mvc
@user281180: `new UriBuilder(Request.Uri.Scheme, Request.Uri.Host, Request.Uri.Port).ToString()` should do it.
Richard