在 ASP.NET MVC 视图中呈现 HTML 文件?
问题描述:
在我看来,想将 HTML 文件的内容呈现为局部视图.但是当我将它添加到 .cshtml 视图时,它给了我这个错误:
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"))
错误:
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?
答
您不能为此使用 Html.Partial
.它是一种特殊的渲染 Partial Views 的辅助方法.相反,您可以像这样添加 Action
:
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");
}
您可以使用 Html.Action
辅助方法:
And you can call it from your View
with using Html.Action
helper Method:
@Html.Action("GetHtmlPage","controllername", new { path = "~/Test/main.html" })