asp.net MVC

Load html page in ASP.NET MVC view

Load html page in ASP.NET MVC view, someone asked me to explain?

In this example, I will show you how to load an html page in asp.net MVC view. You can call an html page from your view using Html.Action helper method.

In controller action you can return an HTML file by using FilePathResult; it will return the content of the file to the response. To prevent from vulnerability you could use childActionOnly attribute to the controller.

Step 1: Right click on the "Controllers" folder and add "LoadHtml" controller. Copy and paste the following code.

  public class LoadHtmlController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [ChildActionOnly]
        public ActionResult GetHtmlPage(string path)
        {
            return new FilePathResult(path, "text/html");
        }
    }

Step 2: Right click on the "Index" action method in the "LoadHtmlController" and add "Index" view. Copy and paste the following code.

@{
   ViewBag.Title = "Loadhtml page in ASP.NET MVC view";
}
<h2>Load html page in ASP.NET MVC view</h2>
@Html.Action("GetHtmlPage","LoadHtml", new { path = "~/add.html" })

Step 3: Right click on the project and create html file "add.html". Copy and paste the following code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
        <img class="temp-logo" src="http://www.infinetsoft.com/Images/logoinfi.png" title="learn infinite software skills through infinetsoft.com" alt="infinetsoft company logo">
</body>
</html>


Note
:
 While running the project If you get the following  character displayed on the top of the html page.  Please refer here.

Output:

Load html page in ASP.NET MVC view

Post your comments / questions