ASP.NET MVC - Find Absolute Path to the App_Data folder from Controller

Posted by tyndall on Stack Overflow See other posts from Stack Overflow or by tyndall
Published on 2009-08-12T21:06:30Z Indexed on 2010/04/10 10:33 UTC
Read the original article Hit count: 924

Filed under:
|
|

What is the correct way to find the absolute path to the App_Data folder from a Controller in an ASP.NET MVC project? I'd like to be able to temporarily work with an .xml file and I don't want to hardcode the path.

This does not work:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        string path = VirtualPathUtility.ToAbsolute("~/App_Data/somedata.xml");

        //.... do whatever 

        return View();
    }

}

I think outside of the web context VirtualPathUtility.ToAbsolute() doesn't work. string path comes back as "C:\App_Data\somedata.xml"

Where should I determine the path of the .xml file in an MVC app? global.asax and stick it an application-level variable?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about .NET