使用处理程序动态创建 JS 文件 ASP.net

Dynamically create JS file using ASP.net Handler

本文关键字:JS 文件 ASP net 创建 动态 处理 程序      更新时间:2023-09-26

我有很多客户,我想给他们脚本,所以我想根据他们的客户ID创建JS文件。所以我可以返回,它直接在客户端执行。客户端可以是任何人,PHP,Html ASP.net

问题是当我浏览此链接时,它给了我 JS 字符串,但在客户端,此脚本没有像测试一样执行,我放了警报,此警报未显示在客户端


客户

<head>
    <script src="http://localhost:12604/JSCreator/Handler.ashx?CustomerID=123" type="text/javascript"></script>
    <title></title>
</head>

处理程序文件

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string CustomerId = context.Request["CustomerId"].ToString();
        string jscontent = JSFileWriter.GetJS(CustomerId); // This function will return my custom js string
        context.Response.ContentType = "text/javascript";
        context.Response.Write(jscontent);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

ContentType应该application/javascript

public void ProcessRequest(HttpContext context)
{
    string CustomerId = context.Request["CustomerId"].ToString();
    string jscontent = JSFileWriter.GetJS(CustomerId); // This function will return my custom js string
    context.Response.ContentType = "application/javascript";
    context.Response.Write(jscontent);
}