gpt4 book ai didi

.net - _ViewStart.cshtml 布局文件在哪里以及如何链接?

转载 作者:行者123 更新时间:2023-12-03 04:11:48 25 4
gpt4 key购买 nike

这是默认 MVC 3 模板中的 About.cshtml:

@{
ViewBag.Title = "About Us";
}

<h2>About</h2>
<p>
Put content here.
</p>

我希望在 About.cshtml 中找到对 _ViewStart 文件的引用,但显然不是。

我查看了 global.asaxweb.config,但我找不到 About.cshtml 文件与 _ViewStart 文件中的布局“链接”。

一切都按预期进行,我只是想知道幕后发生了什么......

最佳答案

来自ScottGu's blog :

Starting with the ASP.NET MVC 3 Beta release, you can now add a file called _ViewStart.cshtml (or _ViewStart.vbhtml for VB) underneath the \Views folder of your project:

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default:

Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of our individual view files (except if we wanted to override the default value above).

Important: Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selection logic richer than just a basic property set. For example: we could vary the Layout template that we use depending on what type of device is accessing the site – and have a phone or tablet optimized layout for those devices, and a desktop optimized layout for PCs/Laptops. Or if we were building a CMS system or common shared app that is used across multiple customers we could select different layouts to use depending on the customer (or their role) when accessing the site.

This enables a lot of UI flexibility. It also allows you to more easily write view logic once, and avoid repeating it in multiple places.

另请参阅this .

关于.net - _ViewStart.cshtml 布局文件在哪里以及如何链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9300410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com