0

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>");
    }
}
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 25 '22 at 23:04
  • ok let me add more. – Hisham Asghar Dec 25 '22 at 23:34
  • Then you should learn to extend IIS, not just ASP.NET, https://learn.microsoft.com/en-us/iis/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework#deploying-the-assembly-to-the-server – Lex Li Dec 26 '22 at 00:32
  • Yes I had already mentioned shared that I had implemented IHttpModule and IHttpHandler, thats what extendablity mean I think so. If its something else then do let me know. Thanks – Hisham Asghar Dec 26 '22 at 03:51

1 Answers1

0

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.

Qiang Fu
  • 1,401
  • 1
  • 2
  • 8