简体   繁体   English

ASP.NET MVC中的静态HTML页面不使用布局

[英]Static HTML page in ASP.NET MVC that does not use Layout

I have an entire Web Application built using ASP.NET MVC and have a _ViewStart.cshtml page that specifies a Layout. 我有一个使用ASP.NET MVC构建的整个Web应用程序,并且有一个指定布局的_ViewStart.cshtml页面。

This works great for my entire site. 这对我的整个网站都很有效。 However, I have a single prototype static HTML page that I just need to dump into a directory. 但是,我只有一个静态HTML原型原型,只需要转储到目录中即可。

I copied the HTML into a CSHTML file and fronted it with a controller. 我将HTML复制到CSHTML文件中,并在其前面添加了控制器。 The problem is that when I go to this page, it is using the Layout. 问题是,当我转到此页面时,它正在使用布局。

How can I configure it so that I can just serve this page up as static, standalone content without the Layout from _ViewStart ? 如何配置它,这样我就可以将此页面作为静态的独立内容提供,而无需使用_ViewStart的Layout?

By default, all views will use the layout from ~/Views/Shared as it is specified in the _Viewstart.cshtml file. 默认情况下,所有视图都会使用_Viewstart.cshtml文件中指定的~/Views/Shared中的_Viewstart.cshtml Every time a view is executed, the code inside the _Viewstart.cshtml will be executed which sets the layout for the view. 每次执行视图时,都会执行_Viewstart.cshtml内部的代码,该代码将设置视图的布局。

If you do not want to execute/include the layout for a specific view, you can explicitly set layout as null on a view. 如果不想执行/包括特定视图的布局,则可以在视图上将布局显式设置为null Add the below code to the view. 将以下代码添加到视图。

@{
    Layout = null;
}

Keep in mind that, even though it is static html in your cshtml file, user will not/should not directly access this view (like a normal html page/htm page). 请记住,即使您的cshtml文件中是静态html,用户也不会/不应直接访问此视图(例如普通的html页面/ htm页面)。 It has to be routed via an action method which returns this cshtml view. 它必须通过操作方法进行路由,该方法返回此cshtml视图。

Another option is to use the PartialView method instead of View method. 另一种选择是使用PartialView方法而不是View方法。 When using the PartialView method to render a view, the framework do not run _ViewStart.cshtml , hence you get the same result. 当使用PartialView方法呈现视图时,该框架不会运行_ViewStart.cshtml ,因此您将获得相同的结果。

public ActionResult About()
{
   return PartialView();
}

PartialView is really handy when you want to render the markup for parts of your page (Ex : content of for a modal dialog etc) 当您要呈现页面各部分的标记时, PartialView非常方便(例如:模态对话框的内容等)

In your static view page set layout = null. 在您的静态视图页面中,将layout设置为null。 Like: @{Layout = null;} 像: @{Layout = null;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM