从注入JS IHttpModule的 [英] Inject js from IhttpModule

查看:153
本文介绍了从注入JS IHttpModule的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用IHttpModule的注入JS页面(到标签)。
但JS没有注入。

我所做的:

页:

 <%@页面语言=C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=MyTempProject._Default%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
< HEAD>
    <标题>温度< /标题>
< /头>
<身体GT;
    <表ID =form1的>
    < D​​IV>    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

的IHttpModule的:

 公共类MyExtensionModule:IHttpModule的
    {
        #区域IHttpModule的成员        公共无效的Dispose()
        {        }        公共无效初始化(HttpApplication的情况下)
        {            context.BeginRequest + =新的EventHandler(context_BeginRequest);
        }
        无效context_BeginRequest(对象发件人,EventArgs的发送)
        {
            HttpContext的背景下=((HttpApplication的)寄件人).Context;
            一页一页= HttpContext.Current.CurrentHandler的页面;
            如果(页面!= NULL)
            {
                字符串脚本=/Scripts/jquery-1.5.1.js;
                如果(page.Header!= NULL)
                {
                    字符串scriptTag =的String.Format(< SCRIPT LANGUAGE = \\JavaScript的\\式= \\文/ JavaScript的\\SRC = \\{0} \\>< / SCRIPT> \\ N,脚本); page.Header.Controls.Add(新LiteralControl(scriptTag));
                }
                否则,如果page.ClientScript.RegisterClientScriptInclude(page.GetType(),脚本,脚本)(page.ClientScript.IsClientScriptIncludeRegistered(page.GetType(),脚本)!);
            }
        }        #endregion
    }


解决方案

的BeginRequest 事件是为时尚早挂接到一个页面。在请求周期这一点上,IIS / ASP.NET甚至还没有决定映射你的要求任何东西。所以,你应该尝试像在波斯特马prequestHandler 事件。

不过,这还不是全部有它:在这一点上的网页(如果有)尚未执行。这种情况发生在 preRequestHandlerExecute PostRequestHandlerExecute 事件之间的权利。所以pre ...尚早,和后...为时已晚。最好的办法是挂钩一个页面事件,如 preRenderComplete ,且有执行你的注入。

 公共无效初始化(HttpApplication的情况下)
{
    context.PostMa prequestHandler + = OnPostMa prequestHandler;
}无效OnPostMa prequestHandler(对象发件人,EventArgs的发送)
{
    HttpContext的背景下=((HttpApplication的)寄件人).Context;
    一页一页= HttpContext.Current.CurrentHandler的页面;
    如果(页面!= NULL)
    {
        页面preRenderComplete + =在preRenderComplete。
    }
}无效在preRenderComplete(对象发件人,EventArgs的发送)
{
    一页一页=(页)发送者;
    //这里脚本注入
}

注意:很少有人还在使用它们,但使用Server.Execute Server.Transfer的不执行任何管道事件的。所以这样子请求不能使用被捕获的 IHttpModule的

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
    }

解决方案

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.

public void Init(HttpApplication context)
{
    context.PostMapRequestHandler += OnPostMapRequestHandler;
}

void OnPostMapRequestHandler(object sender, EventArgs e)
{
    HttpContext context = ((HttpApplication)sender).Context;
    Page page = HttpContext.Current.CurrentHandler as Page;
    if (page != null)
    {
        page.PreRenderComplete += OnPreRenderComplete;
    }
}

void OnPreRenderComplete(object sender, EventArgs e)
{
    Page page = (Page) sender;
    // script injection here
}

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.

这篇关于从注入JS IHttpModule的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆