How to read the contents of a file opened from a virtual path?
I have a static file that exists in a folder in the root of my web application, which gets updated regularly by a 3rd party. I'd like to read the contents of this with a HtmlHelper and inject the contents into my View.
I have the following code:
public static string LoadMyFile(this HtmlHelper htmlHelper)
{
var virtualPath = VirtualPathUtility.ToAbsolute(string.Format("~/XYZ/myFile.asp"));
return "";
}
where virtualPath
contains the value /XYZ/myFile.asp
How can I read the contents of this in my Html Helper, so that I can inject the contents into the View?
note: just to explain, the myFile.asp is something that we have no control over in 开发者_开发百科terms of the contents, the name or when it's updated. We can only control where it exists. As such, we're keeping it in the root of the application so that it can be accessed with the virtual path.
You can use the IO.File utility.
Example File.Open(Server.MapPath("~/XYZ/myFile.asp"))
精彩评论