从IhttpModule注入js
我尝试使用 ihttpmodule 将 js 注入到页面(到标签)。 但js没有注入。
我做了什么:
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTempProject._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Temp</title>
</head>
<body>
<form id="form1">
<div>
</div>
</form>
</body>
</html>
ihttpmodule:
public class MyExtensionModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
string script = "/Scripts/jquery-1.5.1.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);
}
}
#endregion
}
i trying to inject js to page (to tags) by using ihttpmodule.
but js isn't injected.
what i did:
the page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTempProject._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Temp</title>
</head>
<body>
<form id="form1">
<div>
</div>
</form>
</body>
</html>
the ihttpmodule:
public class MyExtensionModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
string script = "/Scripts/jquery-1.5.1.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);
}
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BeginRequest 事件对于挂钩到页面还为时过早。在请求周期的这一点上,IIS/ASP.NET 甚至还没有决定将您的请求映射到任何内容。因此,您可能应该尝试类似 PostMapRequestHandler 事件的操作。
然而,这还不是全部:此时页面(如果有的话)尚未执行。这发生在 PreRequestHandlerExecute 和 PostRequestHandlerExecute 事件之间。所以 Pre... 太早,而 Post... 太晚了。最好的选择是挂钩页面事件,例如 PreRenderComplete,然后执行注入。
注意:很少有人仍在使用它们,但Server.Execute和Server.Transfer不执行任何管道事件 。因此,使用 IHttpModule 永远无法捕获此类子请求。
The BeginRequest event is way too early to hook into a page. At that point in the request cycle, IIS/ASP.NET hasn't even decided to map your request to anything. So you should probably try something like the PostMapRequestHandler event.
However, that's not all there is to it: at that point the page (if there is one) hasn't executed yet. That happens right between the PreRequestHandlerExecute and PostRequestHandlerExecute events. So Pre... is too early, and Post... is too late. Your best bet is to hook a page event such as PreRenderComplete, and there execute your injection.
CAUTION: Few people still use them, but Server.Execute and Server.Transfer do not execute any pipeline events. So such child requests can never be caught using an IHttpModule.