27

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?

1

7 Answers 7

39

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" })
Sign up to request clarification or add additional context in comments.

3 Comments

Beware, this opens up a vulnerability as anyone could use that controller action to get any file on your website. The easiest fix is to apply the [ChildActionOnly] attribute to the controller action so that it can't be accessed directly by the browser.
How can we alter this if we wish to take the result from an actual HTML page from the internet?
@Selman Genç Per @Slight's comment, please edit your answer to include the [ChildActionOnly] attribute so that others won't simply copy and paste your code without looking at the comments. I just tested it without that attribute, and I can look at my web.config, etc.
8

Follow these steps

  1. Create a view in ~/views/shared folder. give it name test.cshtml.
  2. Copy the content of HTML in it.
  3. Use Html.Partial("test") on page to render the html of that view.

Comments

6

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

@Response.WriteFile(pathToMyHtmlFile)

taken from here

Comments

5

The simple answer is to rename the main.html file to main.cshtml which will be recognized by the Razor view engine for rendering.

Comments

3

You could use this in your cshtml view:

@{this.GetOutputWriter().Write(File.ReadAllText(Server.MapPath("/your/static/file.html")));}

Comments

1

A total different way is just load the file in javascript

<div id="loadId">
</div>

<script>
    $(document).ready(function () {
        $('#loadId').load('filepath');
    });
</script>

You can set the filepath in the controller, or put it in the model, if you want, and use:

$('#loadId').load('@ViewBag.filepath');

Comments

0

Just use an ajax request to fetch the html file output the content into your main html. Since it is an html file, it will be cached in subsequent calls and there will be no performance impact.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.