Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render HTML file in ASP.NET MVC view?

In my view, would like to render the contents of an HTML file as a partial view. It is giving me this error though when I add this to the .cshtml view:

@Html.Partial(Url.Content("~/Test/main.html")) 

Errors:

Exception Details: System.InvalidOperationException: The partial view '/Scripts/main.html' was not found or no view engine supports the searched locations. The following locations were searched: /Scripts/main.html 

The file is physically there though. Is there a different way I should be doing this?

like image 862
TruMan1 Avatar asked Jan 01 '14 18:01

TruMan1


People also ask

What is rendering in ASP NET MVC?

Render actions are methods that the view calls back in the controller. A strong design point about MVC is the neat separation of controller and view. In this regard, render actions just break this separation. Render actions are effective to use; balancing design with effective solutions is the developer's job.

What is rendering in asp net?

The Render method is responsible for creating the text and markup that is sent to the client browser. The default Render method calls RenderChildren to write the text and markup for the controls contained on the page.

What is ASP Net mvc6?

ASP.NET MVC is a web application framework developed by Microsoft that implements the model–view–controller (MVC) pattern. It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary. ASP.NET MVC.


2 Answers

You can't use Html.Partial for this.It is a special helper method for rendering Partial Views. Instead you can add an Action like this:

[ChildActionOnly] public ActionResult GetHtmlPage(string path) {    return new FilePathResult(path, "text/html"); } 

And you can call it from your View with using Html.Action helper Method:

@Html.Action("GetHtmlPage","controllername", new { path = "~/Test/main.html" }) 
like image 108
Selman Genç Avatar answered Sep 17 '22 17:09

Selman Genç


I think it's a better solution: Use WriteFile from the Response object

@Response.WriteFile(pathToMyHtmlFile) 

taken from here

like image 40
Mahmood Dehghan Avatar answered Sep 16 '22 17:09

Mahmood Dehghan