开发者

how to serve javascript wtih asp.net page?

开发者 https://www.devze.com 2023-03-23 22:13 出处:网络
I don\'t know if the title is clear enough or not, but let me explain what I\'m trying to do. I have two webapps with written in asp.net with C#.

I don't know if the title is clear enough or not, but let me explain what I'm trying to do.

I have two webapps with written in asp.net with C#.

App A has the following html.

<script type="text/javascript" id="blah" src="http://somServer/AppB/page.aspx?p=q"></script>

App B receives above request and needs to inject javascript dynamically to the script tag above. I have the following code in App B's page.aspx but it doesn't work. I need App B to return pure javascript, not html.

namespace AppB
{
    public partial class Default : System.Web.UI.Page
    {
       if(!Page.IsPostBack)
       {
         Response.Clear();
         Response.ClearContent();
         REsponse.ClearHeader();
         Response.AddHeader("content-type", "text/javascript");
         var p = Request.Query["p"];
         if(!string.IsNullOrEmpty(p))
         开发者_C百科{
            this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "alert('test');", false");
         }
     }
}


You might want to use a HttpHandler rather than Page (see http://support.microsoft.com/kb/308001) to serve non-HTML content. This would allow you to write something like:

public class JavascriptHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/javascript";
        string p = context.Request.QueryString["p"];
        string script = String.Format("alert('test - p={0}');", p);
        context.Response.Write(script);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消