Home » Html » How to serve html file from another directory as ActionResult

How to serve html file from another directory as ActionResult

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a specialised case where I wish to serve a straight html file from a Controller Action.

I want to serve it from a different folder other than the Views folder. The file is located in

Solution\Html\index.htm

And I want to serve it from a standard controller action. Could i use return File? And
how do I do this?

Answers:

If you want to render this index.htm file in the browser then you could create controller action like this:

public void GetHtml()
{
    var encoding = new System.Text.UTF8Encoding();
    var htm = System.IO.File.ReadAllText(Server.MapPath("/Solution/Html/") + "index.htm", encoding);
    byte[] data = encoding.GetBytes(htm);
    Response.OutputStream.Write(data, 0, data.Length);
    Response.OutputStream.Flush();
}

or just by:

public ActionResult GetHtml()
{
    return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/html"); 
}

So lets say this action is in Home controller and some user hits http://yoursite.com/Home/GetHtml then index.htm will be rendered.

EDIT: 2 other methods

If you want to see raw html of index.htm in the browser:

public ActionResult GetHtml()
{
    Response.AddHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { Inline = true, FileName = "index.htm"}.ToString());
    return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/plain"); 
}

If you just want to download file:

public FilePathResult GetHtml()
{
    return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/html", "index.htm"); 
}

Questions:
Answers:

Check this out :

    public ActionResult Index()
    {
        return new FilePathResult("~/Html/index.htm", "text/html");
    }

Questions:
Answers:

I extended wahid’s answer to create HtmlResult

Create Html Result which extends FilePathResult

public class HtmlResult : FilePathResult
{
    public HtmlResult(string path)
        : base(path, "text/html")
    {
    }
}

Created static method on controller

public static HtmlResult Html(this Controller controller, string path)
{
    return new HtmlResult(path);
}

used like we return view

public HtmlResult Index()
{
    return this.Html("~/Index.html");
}

Hope it helps

Questions:
Answers:

Can you read the html file in a string and return it in action? It is rendered as Html page as shown below:

public string GetHtmlFile(string file)
{
    file = Server.MapPath("~/" + file);
    StreamReader streamReader = new StreamReader(file);
    string text = streamReader.ReadToEnd();
    streamReader.Close();
    return text;
}

Home/GetHtmlFile?file=Solution\Html\index.htm

If the destination or storage mechanism of HTML files is complicated then you can you Virtual path provider

Virtual path provider MVC sample

Questions:
Answers:

I want put my two cents in. I have found this most terse and it is there already :

public ActionResult Index()
{
     var encoding = new System.Text.UTF8Encoding();
     var html = ""; //get it from file, from blob or whatever
     return this.Content(html, "text/html; charset=utf-8");
}