Using Server.MapPath in MVC3

前端 未结 3 1085
情深已故
情深已故 2020-12-24 07:39

I have the code

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

It returns

3条回答
  •  我在风中等你
    2020-12-24 08:09

    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.

提交回复
热议问题