Using Server.MapPath in MVC3
Asked Answered
A

3

23

I have the code

string xsltPath = System.Web.HttpContext.Current.Server.MapPath(@"App_Data") + "\\" + TransformFileName

It returns

C:\inetpub\wwwroot\websiteName\SERVICENAME\App_Data\FileName.xsl

Why am I getting the path to the ServiceController, SERVICENAME? I want the path to App_Data which is in

C:\inetpub\wwwroot\websiteName\App_Data\FileName.xsl

Accountant answered 29/9, 2011 at 16:0 Comment(0)
V
57

You need to specify that you want to start from the virtual root:

string xsltPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(@"~/App_Data"), TransformFileName);

Additionally, it's better practice to use Path.Combine to combine paths rather than concatenate strings. Path.Combine will make sure you won't end up in a situation with double-path separators.

EDIT:

Can you define "absolute" and "relative" paths and how they compare to "physical" and "virtual" paths?

MSDN has a good explanation on relative, physical, and virtual paths. Take a look there.

Vernettaverneuil answered 29/9, 2011 at 16:3 Comment(3)
This works. I was reading an article about the "virtual root", but I didn't follow it well. dotnetperls.com/mappath Can you define "absolute" and "relative" paths and how they compare to "physical" and "virtual" paths?Accountant
@P.Brian.Mackey: Absolute means the path from the root, such as "/someTopDirectory/someSubDirectory". Relative means a path from where you are currently at, so if you are in "someSubDirectory" and want to get to "someTopDirectory", a relative path is ".." (go up one directory). Physical paths are paths as the system sees them: "C:\inetpub\myApp\someTopDirectory\someSubDirectory". Virtual paths are as a web server application sees them: "/someTopDirectory/someSubDirectory".Moxa
@P.Brian.Mackey: To clarify more for your situation, MapPath converts a virtual path to a physical path. In your parameter for the virtual path, you want to provide an absolute path instead of a relative one. The marker to indicate the "root" for the absolute path in a web application is "~", so the absolute virtual root = "~/".Moxa
M
7

The answers given so far are what you are looking for, but I think, in your particular case, what you actual need is this:

AppDomain.CurrentDomain.GetData("DataDirectory").ToString()

This will still return the file path to the App_Data directory if that directory name changes in future versions of MVC or ASP.NET.

Moxa answered 29/9, 2011 at 16:9 Comment(2)
I don't think that this is a particularly good idea. You are relying on an undocumented (officially) AppDomain parameter, it's an implementation detail. It is far more likely to change than App_Data.Vernettaverneuil
It is documented, and standard, but it's for ADO.Net, not ASP.Net in particular. DataDirectory won't change but it may not always point to App_Data.Tinnitus
C
0

Try doing like this (@"~/App_Data"). ~/ represents the root directory.

Condescendence answered 29/9, 2011 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.