ASP.NET MVC 3 中 ViewBag.Title 的替代方案
默认情况下,ASP.NET MVC 3 的新项目模板将以下内容添加到默认布局(razor 中的母版页):
<title>@ViewBag.Title</title>
然后,视图必须包含以下内容来分配页面标题,例如:
@{
ViewBag.Title = "Log On";
}
也许这只是我自己的偏好但我发现使用 ViewBag 来保存标题有点错误(我认为太多了魔术字符串的味道)。所以我的问题是:对于使用 ASP.NET MVC 3 和 razor(使用动态属性包)的人来说,这是推荐的最佳实践吗?或者您是否选择更强类型的东西(可能涉及自定义基类?)
By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor):
<title>@ViewBag.Title</title>
The view must then contain the following to assign the title of the page, for instance:
@{
ViewBag.Title = "Log On";
}
Perhaps it is just my own preference but I find that using the ViewBag to hold the title a little bit wrong (I'm thinking too much magic-string flavor). So my question is: Is this the recommended best practice for people using ASP.NET MVC 3 and razor (using a dynamic property bag) or are you opting for something more strongly typed (perhaps involving a custom baseclass?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不认为 ASP.NET MVC 3 附带的默认标题处理功能有什么不好,可以这样做。
我个人这样做(下面写)来处理标题,我不认可下面的代码或说它比默认功能更好,它只是一个偏好。
Master
View
我可以建议改进默认功能的一件事
因此,每当您忘记将其添加到 viewbag 中时,页面都会显示 title=
Title Not Set
创建一个基类,然后创建所有从该基类继承的控制器也可以完成。但我认为它为
title
带来了很大的痛苦。I don't think there is any thing bad with default title handling feature that ships with asp.net MVC 3, its okay to do.
I personally do this(below written) to handle title, I am not endorsing the below code or saying that its better than default functionality, its just a preference.
Master
View
One thing i could suggest to improve default functionality
So whenever you forget to add it in viewbag, the page will display title=
Title Not Set
Creating a base class then making all your controllers inherit from that base-class can also be done. But I think its taking so much of pain for
title
.标题的 ViewBag 完全没问题(我什至会说这是拥有 ViewBag 的目的) - 动态并不是绝对的邪恶。 “标题”是众所周知的,不太可能改变,甚至在视图模板中预定义。我个人使用以下标题:
如果您担心输入错误
ViewBag.Title
,您可以通过创建自定义WebViewPage
使其成为强类型,但您仍然必须使用ViewBag
或者可能是该强类型属性内的HttpContext.Items
,因为在渲染 IIRC 期间创建了多个WebViewPage
实例。我建议坚持使用
ViewBag
,创建自己的WebViewPage
,因为这看起来有点过分 - 如果您已经有自定义WebViewPage
,甚至可以在其上创建单个属性code> 在我看来只是毫无价值的复杂化——而这来自于经常过度设计事物的人。ViewBag for title is perfectly fine (I'd even say it is the purpose of having ViewBag) - dynamic is not the absolute evil. "Title" is well known, unlikely to change and even predefined in view templates. I personally use following title:
If you are worried about mistyping
ViewBag.Title
you can make it strong type by creating customWebViewPage
but you will still have to useViewBag
or maybeHttpContext.Items
inside that strongly typed property because there are multiple instances ofWebViewPage
created during rendering IIRC.I'd recommend to stick with
ViewBag
, creating ownWebViewPage
because of this seems like overkill - even creating single property on it if you already have customWebViewPage
is in my opinion just worthless complication - and that comes from person that is often overengineering things.我也根本不使用 ViewBag。
在 _Layout.shtml 的顶部 ...
在 _Layout.shtml 中 ...
在您的模型中 ...
在 EveryPageViewModel 中
在您的控制器操作中
I also do not use the ViewBag, at all.
At the top of _Layout.shtml ...
In _Layout.shtml ...
In your Model ...
In EveryPageViewModel
In your controller action
我喜欢创建 PageTitle ActionFilter 属性,而不是编辑单个 ViewBags
用法:保持视图相同
对于控制器范围的页面标题:
对于单个视图:
属性代码:
易于管理,工作起来就像一个魅力。
I like to create a PageTitle ActionFilter attributes rather than editing individual ViewBags
Usage: keep the view the same
For controller-wide page title:
For individual views:
Attribute Code:
Easy to manage and works like a charm.
就我个人而言,我认为这种情况是
ViewBag
的可接受使用。它仅限于“众所周知”的属性,并且将来可能不会造成任何问题。最后,一切都是为了务实并找到尽可能快的方法。在我看来,拥有一个需要设置标题的基类代码太多,不值得类型安全。祝你好运!
For me personally, I think this case is an acceptable use of
ViewBag
. It is limited to a "well-known" property, and it likely won't cause any problems in the future. In the end, it is all about being pragmatic and finding a way to be as quick as possible. Having a base class where you need to set the title would in my opinion be too much code to be worth the type safety.Good luck!
我想说只要你想设置的只是Title,用ViewBag就可以了。好吧,也许不仅如此——最多 2-3 个属性。
但是,如果您开始发现在每个控制器操作中设置越来越多的(通用)属性,我会选择强类型的“ViewModelBase 类”。但这只是我。
I would say that as long as it's only the Title you want to set, it's ok to use the ViewBag. Well, maybe not only - at most 2-3 properties.
But if you start to see you're setting more and more (common) properties in every controller action, I would go with a strongly typed "ViewModelBase class". But it's just me.
我们更喜欢强大的标题设置......来自我们的 BaseController 类的一些示例。 (页面定义封装视图模式)
we prefer strong title setting.. few sample from our BaseController class. (Page defines encapsulated view modal)
使用 ViewBag.Title = "My Title"; 没有任何问题。
您所做的就是使用动态属性。
问题实际上是信息应该在哪里“声明”。
即,对于当前的目的来说,它在哪里最容易实现。
如果它是在每页的基础上,那么那就是正确的地方。
但是,如果页面标题可以从模型派生,那么您应该这样做。
在这种情况下,我可能会使用您使用的 ViewModel 的基类,并在其中创建一个 PageTitle 属性,其中包含从模型中的属性派生页面标题的逻辑。
所以:
总之,马匹适合课程,不要害怕使用动态属性......只要您了解它们是什么以及它们做什么。
There is nothing wrong with using ViewBag.Title = "My Title";
All you are doing is use a dynamic property.
The question is really where the information should be "declared".
Ie, where is it most accessible for the purposes at hand.
If it is on a per page basis, then that is the right place.
If, however, the title of the page can be derived from the Model, then you should do that.
In this case, I would probably use a base class for the ViewModel that you use, and create a PageTitle property there that contains the logic to derive the page title from properties in the Model.
So:
In summary, horses for courses and don't be afraid of using dynamic properties... so long as you understand what they are and what they do.