How to insert code in the static files like .html, .htm, .asp on IIS for non-asp project

106 Views Asked by At

I want to add a script on my IIS Server.

So that it will be applied on all the websites that are upload will have that script in their request response.

Anyone who knows how to do it?

I had implemented the IHttpModule and IHttpHandler, it works fine for the asp.net projects.

but if the website contains only html, css, and js files in the folder, this solution doesn't work.

Here the HttpModule and HttpHandler

public class MyCustomHttpModuleClass : IHttpModule
{
    public void Dispose()
    {
    }
    public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
    }
    public void OnPostRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        HttpContext context = application.Context;
        context.Response.Write("<h1>alert('HELLO')</h1>");
    }
}

public class MyHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("<h1>alert('HELLO')</h1>");
    }
}
1

There are 1 best solutions below

0
On

I'm not sure if you have learnt how to add Custom Module and Handler in IIS. After tested your module and handler with static website, it works fine. I will just give you a sample of adding them to IIS.

1.Create a project "class library .netframework". I name the project"ClassLibrary1" enter image description here

2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project enter image description here 3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder.

4.Copy "ClassLibrary1.dll" to the website root "BIN" folder.

5.Add managed module and handler by choose your dll.(should in the list after you copied)Just mention that your custom handler only work on the file extension you set up. enter image description here

enter image description here Now they work.