开发者

Using Server.MapPath in MVC3

开发者 https://www.devze.com 2023-04-09 03:27 出处:网络
I have the code string xsltPath = System.Web.HttpContext.Current.Server.MapPath(@\"App_Data\") + \"\\\\\" + TransformFileName

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 p开发者_如何学JAVAath to App_Data which is in

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号