asp.net-mvc 在 ASP.NET MVC 中禁用布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/220342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable layout in ASP.NET MVC?
提问by Aaron Jensen
In MonoRail you can just CancelLayout() to not render the layout. In ASP.NET MVC, the only way to affect the layout seems to be to pass the layout name into the View() method like View("myview", "mylayout"); only it seems that passing null or an empty string doesn't do what I'd want.
在 MonoRail 中,您可以仅使用 CancelLayout() 来不呈现布局。在 ASP.NET MVC 中,影响布局的唯一方法似乎是将布局名称传递给 View() 方法,如 View("myview", "mylayout"); 只是似乎传递 null 或空字符串不会做我想要的。
I ended up creating an empty layout that just rendered the content, but that seems silly.
我最终创建了一个只呈现内容的空布局,但这似乎很愚蠢。
"Not Render the layout" means exactly that. In the web forms view engine they call layouts "master pages". I want to render justmy action's view and not surround it with the master page.
“不渲染布局”就是这个意思。在 Web 表单视图引擎中,他们将布局称为“母版页”。我想渲染只是我的行为的观点,而不是与母版页围绕着它。
采纳答案by Aaron Jensen
It seems this was impossible in the version of ASP.NET MVC I was asking about.
在我询问的 ASP.NET MVC 版本中,这似乎是不可能的。
回答by expdiant
In MVC 3, you can remove the master layout code with:
在 MVC 3 中,您可以删除主布局代码:
@{
Layout = "";
}
回答by berzinsu
At the beginning of view add this:
在视图的开头添加:
@{
Layout = null;
}
If you want style sheet to remain, you'll need to add reference to it in that view.
如果您希望保留样式表,则需要在该视图中添加对它的引用。
回答by Chris Halcrow
To disable this for all pages, edit the _ViewStart.cshtml (in the root, under the 'Views' folder), and ensure that it contains the following:
要对所有页面禁用此功能,请编辑 _ViewStart.cshtml(在根目录中的“Views”文件夹下),并确保它包含以下内容:
@{
Layout = null;
}
And to enable the template for any specific view, the following can be added to the .cshtml file for that view, to enablethe template:
并且要为任何特定视图启用模板,可以将以下内容添加到该视图的 .cshtml 文件中,以启用模板:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
回答by TomCat
In the Controller action we can set the required layout.
在 Controller 操作中,我们可以设置所需的布局。
return View("Index", "_LAYOUT_NAME", model);
回答by Fenton
Instead of using a normal view, create a partial view. These can then be used on their own, which acts very much like CancelLayout() - or you can incorporate them into a view that references the Master Page, in which case it will be the full layout. They are also useful if you want to send back a partial HTML chunk in response to an AJAX request.
不要使用普通视图,而是创建局部视图。然后可以单独使用它们,其作用与 CancelLayout() 非常相似 - 或者您可以将它们合并到引用母版页的视图中,在这种情况下,它将是完整的布局。如果您想发回部分 HTML 块以响应 AJAX 请求,它们也很有用。
回答by Simon Weaver
Not having any luck trying to set the masterPageparameter to ""or nulland returning a View(like I didn't)?
尝试将masterPage参数设置为""ornull并返回一个View(就像我没有)没有任何运气?
Then try this and use PartialViewinstead:
然后试试这个并使用PartialView:
public ActionResult Article(string id)
{
return PartialView("~/Areas/Store/Views/CustomerService/" + id);
}
I needed to do this to load the contents of a view asynchronously from JS.
我需要这样做以从 JS 异步加载视图的内容。
回答by lat94
I see in the right answer it says that "It seems this was impossible in the version of ASP.NET MVC"
我在正确的答案中看到它说“这在 ASP.NET MVC 版本中似乎是不可能的”
Which version are you using? Because I found the solution (I had the same issue) to your problem
您使用的是哪个版本?因为我找到了您的问题的解决方案(我遇到了同样的问题)
So, to Disable Layout in the page, you should use:
因此,要在页面中禁用布局,您应该使用:
@{
Layout = null;
}
And, as suggested here, this could solve your problem:
而且,正如这里所建议的,这可以解决您的问题:
public ActionResult Index()
{
SampleModel model = new SampleModel();
//Any Logic
return View("Index", "_WebmasterLayout", model);
}
回答by Aaron Jensen
You can create a custom ActionResult that does pretty much anything. The ActionResult controls what is sent back to the client as the response. It would be trivial to create a class that extends ActionResult that does nothing.
您可以创建一个几乎可以执行任何操作的自定义 ActionResult。ActionResult 控制作为响应发送回客户端的内容。创建一个扩展 ActionResult 什么都不做的类将是微不足道的。

