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
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
Related Article