简体   繁体   English

如何在 static 文件中插入代码,如 .html、.htm、.asp on IIS for non-asp project

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

I want to add a script on my IIS Server.我想在我的 IIS 服务器上添加一个脚本。

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.我已经实现了 IHttpModule 和 IHttpHandler,它适用于 asp.net 项目。

but if the website contains only html, css, and js files in the folder, this solution doesn't work.但如果网站文件夹中只有html、css和js文件,这个方案就不行了。

Here the HttpModule and HttpHandler这里是 HttpModule 和 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>");
    }
}

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.不知道你有没有在IIS中学习到如何添加Custom Module和Handler。在static网站上测试了你的模块和handler后,它工作正常。 I will just give you a sample of adding them to IIS.我会给你一个将它们添加到 IIS 的示例。

1.Create a project "class library .netframework". 1.创建一个项目“类库.netframework”。 I name the project"ClassLibrary1"我将项目命名为“ClassLibrary1” 在此处输入图像描述

2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project 2.在项目中添加class“MyCustomHttpModuleClass”和“MyHandler” 在此处输入图像描述 3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder. 3.构建此解决方案并在“project/bin/debug”文件夹中找到“ClassLibrary1.dll”。

4.Copy "ClassLibrary1.dll" to the website root "BIN" folder. 4.将“ClassLibrary1.dll”复制到网站根目录“BIN”文件夹中。

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. 5.通过选择您的 dll 添加托管模块和处理程序。(复制后应该在列表中)请注意您的自定义处理程序仅适用于您设置的文件扩展名。 在此处输入图像描述

在此处输入图像描述 Now they work.现在他们工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM