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
- Fix-Gradient effect turning to gray in after effects
- How to blur an image in python?
- ModuleNotFoundError: No module named 'whois' in Python GoviralHost Without Terminal
- How to Convert Image to Pencil Sketch in Python?
- AttributeError: module 'urllib' has no attribute 'request' - Python
- How to Extract audio from video files using python?
- PermissionError: [Errno 13] Permission denied: 'shampoo_sales.csv' - Python
- [WinError 145] The directory is not empty: 'FolderPath' - Python
Related Article