Asp.net

在 ASP.NET MVC 視圖中呈現 HTML 文件?

  • January 1, 2014

在我看來,希望將 HTML 文件的內容呈現為部分視圖。當我將它添加到 .cshtml 視圖時,它給了我這個錯誤:

@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

該文件雖然物理存在。我應該這樣做有不同的方式嗎?

你不能用Html.Partial這個。它是渲染局部視圖的特殊輔助方法。相反,您可以添加Action這樣的:

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

你可以View使用Html.Actionhelper 方法從你的呼叫它:

@Html.Action("GetHtmlPage","controllername", new { path = "~/Test/main.html" })

引用自:https://stackoverflow.com/questions/20871938