Inject js from IhttpModule

早过忘川 提交于 2019-12-04 11:30:14

问题


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
    }

回答1:


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.



来源:https://stackoverflow.com/questions/5239605/inject-js-from-ihttpmodule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!