MVC HtmlHelpers:可以写入页眉吗?
我是一名 Web 表单程序员,有兴趣了解一些 MVC。我创建了一整套 Web 控件,可以直接访问页面标题并在其中写入内容。例如,如果我有一个控件来呈现具有日期选择器功能的文本框,它可以访问页面标题,以便自动添加指向其所需的 JavaScript 和 CSS 文件的链接。我喜欢这个,因为我懒得去想我需要什么链接文件。懒惰的程序员就是好程序员,对吗?
我的问题是,有没有办法在 MVC 中做到这一点?也就是说,要创建一个自定义 HtmlHelper(例如),它除了在页面上呈现控件标记之外,还可以将其所需的脚本和链接标记呈现到页眉中?
I'm a web forms programmer who's interested in learning a bit about MVC. I have created a whole suite of web controls which can access the page header directly and write stuff in there. For example, if I have a control to render a text box with datepicker functionality, it can access the page header in order to automatically add links to the JavaScript and CSS files it needs. I love this, because I'm too lazy to think about what linked files I need. A lazy programmer is a good programmer, right?
My question is, is there a way to do this in MVC? That is, to create a custom HtmlHelper (for example) which, as well as rendering the control markup on the page, can render the script and link tags it requires into the page header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的
_Layout.cshtml
(基本上是母版页)中使用 MVC3,您可以使用@Render.Partial("Header", required: false)
然后您可以在视图中使用命名部分。
使用
required: false
这意味着如果视图没有 Header 的命名部分,MVC3 不会出错。如果您想要求所有页面都有一个命名部分(例如面包屑),您可以使用required: true
(这是默认值),并且如果视图没有@section header
会出错。With MVC3 in your
_Layout.cshtml
(basically master pages) you use@Render.Partial("Header", required: false)
Then you can use named sections in your views.
With
required: false
this means MVC3 won't error if the view doesn't have a named section for Header. If you want to require ALL pages have a named section (for example bread crums) you can userequired: true
(which is the default) and if a view doesn't have the@section Header
it would error.在 Web 表单中,您有自定义服务器控件,它获取页面对象并使用它? Asp.net mvc没有服务器控件和Page对象。
对我来说,以这种方式链接资源是个坏主意——在客户端优化最佳实践中——向服务器打开最少数量的资源请求。您可以下载 YSlow 来提高网站的性能。
但如果您愿意,您可以创建扩展方法并在视图中使用它或创建 actionfilterattribute,它设置一些 url 字符串;
In web forms you have custom server controls, which get page object and work with it? Asp.net mvc does not have server controls and Page object.
As for me it's bad idea to link resources such way - in client optimization best practice - open the least number of requests to the server for resources. You could download YSlow to improve perfomance of your site.
But if your want you could create extension method and use it in view or create actionfilterattribute, which set some url strings;
是的,Telerik 通过其 ScriptRegistrar 和 Stylesheet 注册器在这方面做得很好。一个好的起点是 web资产文档
Yes, - Telerik does a great job of doing this with its
ScriptRegistrar
andStylesheet
registrar. A good starting point is the web asset documentation我在互联网上找到了一种脚本管理器
,该资源部分与您的问题相关,并且可能是在这方面可以做的最多的事情。
该解决方案并不真正在
HEAD
元素中渲染,但它有助于从部分视图中仅添加一次脚本并将其渲染在主视图中。因此您不必担心部分视图脚本。他们会照顾好自己(即更加封装)A Simple ScriptManager for ASP.NET MVC
自定义视图引擎方法
但除此之外,我认为可以通过将引用保存在字典中(类似于上面的链接的方式),然后拥有自己的视图引擎在最终渲染脚本引用中渲染头部部分。
自定义
ViewPage
类方法您也可以为视图和部分视图编写您赢得的类。通过这种方式,您可以更改视图呈现的方式,从而再次使用某种字典。您可以通过
Html
辅助扩展方法填充该字典,或者创建一个具有此类功能的自定义ViewUserControl
类。在这三个中,我认为最后一个是最简单的,可以在
HEAD
元素中编写。您还可以在其中提供进行资源组合的功能。A sort of script manager
I've found this resource on the internet that's partially related to your problem and may be the most that can be done in this regard.
The solution doesn't really render in the
HEAD
element but it helps adding scripts only once from within partial views and rendering them in the main view. So you wouldn't have to worry about your partial view scripts. They'd take care of themselves (being more encapsulated that is)A Simple ScriptManager for ASP.NET MVC
Custom view engine approach
But otherwise I suppose it could be possible by saving references in a dictionary (similar to how the upper link does) and then have your own view engine that renders head part in the end rendering script references.
Custom
ViewPage
class approachYou could as well write your won classes for views and partial views. This way you could change the way that a view renders which would again use some sort of dictionary. You could fill that dictionary via
Html
helper extension method or create a customViewUserControl
class that would have this kind of functionality.Of the three I suppose the last one is the simplest and could write inside
HEAD
element. You could also provide functionality in it that would do resource combining.