Asp.Net MVC 中的样式和脚本管理
我现在在我的项目中使用 asp.net mvc 2.0。我喜欢 ASP.NET MVC 的设计和工作方式,但我觉得它缺乏对具有大量样式和脚本的应用程序的良好支持。
为了隔离应用程序的不同部分,我使用区域、仅限子操作和部分操作。我也想要一种标准方法来分离我的脚本和样式,即将我的脚本和样式文件放在一个区域中(而不是放在根目录的脚本和内容文件夹中),以及将它们包含在它们所属的页面上的好方法。
现在,我在每个页面上使用内容占位符,我可以在其中包含该操作的任何脚本和样式,但我想要一种包含来自例如仅限儿童操作的脚本的方法。
您对此有何想法,如何管理脚本和样式文件,是否有任何模式或最佳实践?
I'm right now using asp.net mvc 2.0 in my projects. I like the way asp.net mvc is designed and works, but i feel it lack good support for applications with a large amount of styles and scripts.
In order to isolate different parts of an application i use areas, child only actions and partials. I would like a standard way to separate my scripts and styles too, i.e. put my script and style files in an area (not in the script and content folder on the root) and good way of including them on pages where they belong.
Right now I'm using a content place holder on every page in which I can include any script and styles for that action, but I would like a way of including a script from e.g. child only action.
What are your thoughts on this, how do you manage your script and style files, are there any patterns or best practices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以使用 Chirpy(嗯哼):
http://chirpy.codeplex.com/
或 SquishIt:
https://github.com/jetheredge/SquishIt
或者创建一个处理 js/css 请求的控制器文件:
http ://www.weirdlover.com/2010/05/10/zcontroller-asp-net-mvc-controller-fo-your-scripts-styles-images/
或者创建一个 IHttpModule 来处理 js/css 请求:
How2:什么事件挂钩 HttpModule 以将 js 链接放入 head 元素
不确定这是否是您正在寻找的。但这就是我所拥有的一切。
You could use Chirpy (ehem):
http://chirpy.codeplex.com/
Or SquishIt:
https://github.com/jetheredge/SquishIt
Or create a controller that handles requests for js/css files:
http://www.weirdlover.com/2010/05/10/zcontroller-asp-net-mvc-controller-fo-your-scripts-styles-images/
Or create an IHttpModule for handling js/css requests:
How2: what event to hook in HttpModule for putting js links into head element
Not sure if that's what you're looking for. But it's all I've got.
我使用了我第一次看到的注册脚本/css的风格 此处。您在代码中注册脚本,然后将它们缩小、混合、缓存并包含在一个位置。我扩展了这种方法以允许对脚本进行分组。因此,您可以在“默认”组中包含“母版页”脚本,这些脚本将输出到您的母版页上。子页面可以注册您自己的组。
如果做得好,它会非常优雅,你会得到显着的性能提升,并使 YSlow 高兴。
干杯,
I have used a style of registering scripts/css I first saw here. You register your scripts in code and they are then minified, mashed, cached and included in one place. I extending this approach to allow grouping of scripts. So you could include "master page" scripts in your "default" group these would be outputed on your master page. Child pages could register your own groups.
Done well it's very elegant and your get a noticable performance boost and make YSlow happy.
Cheers,
我将每个页面上使用的脚本和样式放在母版中,然后留下内容占位符,以便每个页面都有可能包含所需的脚本和样式。正如你所做的那样。
I put scripts and styles which are used on every page in the master and then leave a content placeholder so that every page has the possibility to include required scripts and styles. Exactly as you do.