Read HTML file from folder and put its content into block of web site using asp.net MVC C#

13,843

Solution 1

You may use System.IO.File.ReadAllText() or StreamReader class methods to read a file from the disk.

For instance, suppose you have sample.html files under the files folder in the root web-app folder.

string path=HttpContext.Current.Server.MapPath("~/files/sample.html");
string content=System.IO.File.ReadAllText(path);

Solution 2

Try this:

<div>
    <% Response.WriteFile("MyFile.html") %>
</div>

Or, if you've got them installed on your web server, try server-side includes:

<!-- #Include virtual="/MyFile.html" -->

Share:
13,843
Nothing
Author by

Nothing

Developer

Updated on June 09, 2022

Comments

  • Nothing
    Nothing almost 2 years

    I have HTML files placed in an HTML folder of my project. The file name of those HTML files are stored in the field of database. I want to read the contents of each HTML file and put it in a <div> on the web page.

    This is what I have done successfully with the image path:

    <img width="135px" src="<%:HelperClass.CheckImageUrlExist(HelperClass.Contact.PathProductImages+Model.Item[i].PictureName)%>" alt="<%: Model.Item[i].Name %>"/>
    

    It is no problem with the file name of image, but with the content of html file, I have no idea.

    Can anyone tell me, how can I do that? Thanks.