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:
Post your comments / questions
Recent Article
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
Related Article