ASP.NET MVC Get file from virtual path

For various reasons, in development I sometimes want to intercept a request, say ~ / MyStyle.css

What I want to do is make the following snippet:

string absFile = VirtualPathUtility.ToAbsolute(file);
return System.IO.File.ReadAllText(absFile);

This absolute path is absolute for the web server, although it will not display on "C: \ whatever". Is there an equivalent method to go to the file system? (Or a ReadFromVirtualPath, etc.?)

+5
source share
2 answers

Use Server.MapPath()to get the file system path for the requested application path.

string absFile = Server.MapPath(file);

or

string absFile = HttpContext.Current.Server.MapPath(file);
+18
source

OpenFile VirtualPathProvider, ,

var stream = HostingEnvironment.VirtualPathProvider.OpenFile(file);
var text = new StreamReader(stream).ReadToEnd();

, , VirtualPathProvider, , , css, .

+6

All Articles