由于各种原因,在开发中我偶尔想拦截对 ~/MyStyle.css 的请求
我想做的是让下面的代码片段起作用:
string absFile = VirtualPathUtility.ToAbsolute(file);
return System.IO.File.ReadAllText(absFile);
虽然这个绝对路径对于网络服务器来说是绝对的,但它不会映射到“C:\whatever”。是否有等效的方法进入文件系统? (或者 ReadFromVirtualPath
等?)
最佳答案
使用 Server.MapPath()
获取请求的应用程序路径的文件系统路径。
string absFile = Server.MapPath(file);
或
string absFile = HttpContext.Current.Server.MapPath(file);
关于c# - ASP.NET MVC 从虚拟路径获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4410962/