使用Html而不是csHtml

时间:2013-08-04 18:36:26

标签: asp.net asp.net-mvc razor

我想使用纯HTML页面而不是使用MVC .net的cshtml。 但是当我通过右键单击索引添加视图时,我只能看到两个选项。

 public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }}
  1. Cshtml(Razor)
  2. .aspx的
  3. 我跟着 Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

    论坛但仍然没有帮助。我仍然没有看到添加html而不是cshtml的选项

    我也尝试将html页面直接添加到视图文件夹,但我不知道如何从我的控制器功能指向该视图。

    用Index.html替换Index.cshtml给了我这个错误

    The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    
    ~/Views/Home/Index.aspx
    ~/Views/Home/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx
    ~/Views/Home/Index.cshtml
    ~/Views/Home/Index.vbhtml
    ~/Views/Shared/Index.cshtml
    ~/Views/Shared/Index.vbhtml
    

5 个答案:

答案 0 :(得分:11)

您可以创建一个带有常规cshtml文件的视图,将其添加到控制器中,在视图中,只需使用纯HTML并将以下内容添加到顶部:

@{
    Layout = null;
}

这样您就可以使用不使用主布局文件的cshtml文件。并且只是提供您放入其中的任何HTML。

答案 1 :(得分:11)

为了呈现纯HTML文件,您可以使用

return new FilePathResult(HtmlPath, "text/html");

HtmlPath是

Server.MapPath(string.Format("~/Views/{0}/{1}.html", YourControllerName, YourHtmlfileName))

答案 2 :(得分:9)

如果它是一个静态html文件,你不需要将控制器指向它,因为IIS可以很好地为它们提供服务。将html文件添加到项目中(任何地方但不在视图文件夹中),并将浏览器指向该文件。

答案 3 :(得分:1)

如果右键单击Content文件夹,则可以选择“添加新项目...”。然后,您可以在左侧的树中选择“Web”,然后选择右侧的“HTML页面”。

这应该为您添加一个HTML页面。

答案 4 :(得分:-2)

添加文件。重命名它。 解决方案资源管理器中的F2。

相关问题