我想让 HttpModule 从一些简单的配置数据中注入 javascripts、css 链接到 HEAD 元素。我不确定我应该挂钩哪个事件?
目前我使用
- context.PreRequestHandlerExecute 动态更改母版页
- Context.BeginRequest 用于 SEO 优化
HTTPModule 事件执行顺序有一些帮助吗?
感谢您的任何提示。干杯,X。
我想让 HttpModule 从一些简单的配置数据中注入 javascripts、css 链接到 HEAD 元素。我不确定我应该挂钩哪个事件?
目前我使用
- context.PreRequestHandlerExecute 动态更改母版页
- Context.BeginRequest 用于 SEO 优化
HTTPModule 事件执行顺序有一些帮助吗?
感谢您的任何提示。干杯,X。
using System;
using System.Web;
using System.Web.UI;
namespace YourNamespace
{
public class YourModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += Application_PreRequestHandlerExecute;
}
private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
string script = "/js/jquery.1.3.2.min.js";
if (page.Header != null)
{
string scriptTag = String.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>\n", script);
page.Header.Controls.Add(new LiteralControl(scriptTag));
}
else if (!page.ClientScript.IsClientScriptIncludeRegistered(page.GetType(), script))
page.ClientScript.RegisterClientScriptInclude(page.GetType(), script, script);
}
}
public void Dispose() { }
}
}
ASP.Net 生命周期:http: //msdn.microsoft.com/en-us/library/ms178473.aspx
以下是我如何在没有 HttpModule 的情况下实现你正在做的事情。我不喜欢 httpmodule 的想法,因为如果我忘记注册它并且它没有运行,我的应用程序将无法运行,这将是一个不明显的错误。该页面绝对需要包含 JS,因此我决定将其放在项目的基 Page 类中。
我是在几年前实现的,希望能够包含的不仅仅是脚本,它还支持 css、元标记等。我忘记了为什么我使用 Page.Header.Controls.Add 而不仅仅是 Page.ClientScript.RegisterClientScriptInclude但这是有原因的。
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
namespace YourNamespace
{
public class HeaderIncludesManager
{
private List<string> m_IncludedFiles = new List<string>();
public void IncludeScript(string s)
{
IncludeScript(s, null);
}
public bool IsIncluded(string file)
{
return (m_IncludedFiles.Find(s => s.Equals(file, StringComparison.InvariantCultureIgnoreCase)) != null);
}
public void IncludeScript(string script, string condition)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (!IsIncluded(script) || page == null)
return;
string scriptFile = string.Format("/{0}/{1}?v={2}", MyConfig.JSDir, script, MyConfig.BuildNumber);
if (page.Header != null)
{
string scriptTag = String.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>\n", scriptFile);
if (!String.IsNullOrEmpty(condition))
scriptTag = String.Format("<!--[{0}]><script language=\"javascript\" type=\"text/javascript\" src=\"{1}\"></script><![endif]-->\n", condition, scriptFile);
page.Header.Controls.Add(new LiteralControl(scriptTag));
m_IncludedFiles.Add(script);
}
else if (!page.ClientScript.IsClientScriptIncludeRegistered(GetType(), scriptFile))
{
page.ClientScript.RegisterClientScriptInclude(GetType(), scriptFile, scriptFile);
m_IncludedFiles.Add(script);
}
}
public void IncludeCss(string css)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (!IsIncluded(css) || page == null)
return;
string cssfile = string.Format("/{0}/{1}?v={2}", MyConfig.CssDir, css, MyConfig.BuildNumber);
if (page.Header != null)
{
((Page)HttpContext.Current.CurrentHandler).Header.Controls.Add(new LiteralControl(String.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\n", cssfile)));
m_IncludedFiles.Add(css);
}
}
public void IncludeJQuery()
{
IncludeScript("jquery-1.2.3.min.js");
}
public void IncludeJQueryUI()
{
IncludeJQuery();
IncludeScript("jquery.ui.1.0.min.js");
IncludeCss("jquery-theme.css");
}
public void IncludeFlotScripts()
{
IncludeJQuery();
IncludeScript("flot/jquery.flot.js");
IncludeScript("flot/excanvas.pack.js", "if IE");
}
}
public class MyPage : Page
{
public HeaderIncludesManager HeaderIncludes = new HeaderIncludesManager();
}
public class MyControl : UserControl
{
public new MyPage Page
{
get
{
return (MyPage)base.Page;
}
}
}
public class SomeControlThatNeedsScripts : MyControl
{
protected override void OnLoad(EventArgs e)
{
Page.HeaderIncludes.IncludeJQueryUI();
base.OnLoad(e);
}
}
}
HttpHandler 将是此类功能的更好选择。这是一个结合 css 和 javascript 文件的示例;它不完全是您正在寻找的东西,但应该让您朝着正确的方向前进:http: //www.codeproject.com/KB/aspnet/HttpCombine.aspx