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 programmatically modifying the AppSetting value in web.config using c#?
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
- How to calculate the age from jQuery ui datepicker using c#?
- How to calculate days difference between two dates in c#?
- Changing date format in jQuery ui datepicker
- How to set value to nicedit textarea using jQuery?
- How to load data from json file in angular?
- cannot find module consider using '--resolvejsonmodule' to import module with json extension angular
Related Article