惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
T
Tenable Blog
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
月光博客
月光博客
美团技术团队
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
NISL@THU
NISL@THU
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Project Zero
Project Zero
Latest news
Latest news
博客园_首页
C
Cisco Blogs
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
Vercel News
Vercel News
Know Your Adversary
Know Your Adversary
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG

博客园 - 双鱼座

永中科技倒闭的另一面 ASP.NET MVC + ADO.NET EF 项目实战(三):引入jQuery 虎年刚开头,给大家拜个年... 每一个社区成员,用你的努力为微软平台的开发技术添上一把柴! POCO真那么重要么? IT风云录(开篇词):我们从David Cutler学到什么? ASP.NET MVC的RAD之路(一) ASP.NET MVC + ADO.NET EF 项目实战(二):设计过程与设计工具 体验ADO.NET Entity Framework的继承 MSDN,微软怎么会这样啊? ASP.NET MVC + ADO.NET EF 项目实战(一):应用程序布局设计 用asp.net mvc写一个后台文件上传 ORM漫谈 对Delphi 7/Delphi 2007的Windows服务类库的一个小改进 案例分析:面向对象得失论 闲话时间调度算法 2007体检结果出来了 在ASP.NET应用程序中捕捉身份验证状态的变化 以非泛型方式调用泛型方法
也说 ASP.NET MVC的 Script 管理
双鱼座 · 2010-01-03 · via 博客园 - 双鱼座

WebForm下的ScriptManager在ASP.NET MVC下自然是不能使用的。于是很多人开始困惑如何管理页面上可能发生冲突的脚本。CodePlex上还有一个项目专门做这件事情,当然也有人简单地通过HtmlHelper来解决。如果你看过jQuery UI Extensions for ASP.NET MVC,或者是jQuery Grid for ASP.NET MVC,你还会找到更多的解决方案。总体上讲,这些解决方案的特点是:

1.用一个词典管理已经注册的脚本项,最后再一次性生成所有注册的脚本。所以,你不能漏了在masterpage的bady的最后运行脚本生成。

2.脚本在页面的body区而不是head区。

思考半天,感觉复杂了一点。我的解决方案比较简单,每次注册脚本调用一个扩展足够了。

代码

public static ScriptManagementExtension
{
        
private const string ScriptFormat = "\t<script src=\"{0}\" type=\"text/javascript\"></script>";
        
private const string CSSFormat = "\t<link href=\"{0}\"  rel=\"stylesheet\" type=\"text/css\"></link>";private static string IncludeHeader(HtmlHelper helper, string key, string path, string format)
        {
            var context 
= helper.ViewContext.HttpContext;
            var exists 
= context.Items.Contains(key);
            
if (!exists)
            {
                var url 
= new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
                context.Items[key] 
= true;
                
return string.Format(format, url.Content(path));
            }
            
return null;
        }
public static string IncludeScript(this HtmlHelper helper, string path)
        {
            
return IncludeScript(helper, path.ToLower(), path);
        }
public static string IncludeScript(this HtmlHelper helper, string key, string path)
        {
            
return IncludeHeader(helper, key, path, ScriptFormat);
        }
public static string IncludeCSS(this HtmlHelper helper, string path)
        {
            
return IncludeCSS(helper, path.ToLower(), path);
        }
public static string IncludeCSS(this HtmlHelper helper, string key, string path)
        {
            
return IncludeHeader(helper, key, path, CSSFormat);
        }
}

使用的时候在masterPage的head区域加入一个占位标记:

<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />

然后在每个view中你都可以通过下面的代码来注册脚本了:

<%= Html.IncludeCSS("http://www.cnblogs.com/Content/Site.css") %>
<%= Html.IncludeCSS("http://www.cnblogs.com/Content/ui.jqgrid.css")%>
<%= Html.IncludeCSS("jQuery_Theme", Html.GetThemePath()) %>
<%= Html.IncludeScript("http://www.cnblogs.com/Scripts/jquery-1.3.2.min.js")%>

...

当然,我也有我的困惑。我的困惑就是,为什么Microsoft没有直接提供Script管理解决方案,抑或是已经提供了,我没有发现?