4

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))
         {
            this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "alert('test');", false");
         }
     }
}
6
  • What kind of errors are you getting? Commented Jul 27, 2011 at 23:03
  • 1
    Why are you calling another .aspx page? Is that a specific requirement? Commented Jul 27, 2011 at 23:05
  • When you say doesn't work...Does the resource load but with the incorrect content type? Can you maybe attach a screengrab of the request/response headers from FireBug. Commented Jul 27, 2011 at 23:14
  • @Infotekka, there is no error but the result is not what I expected. Commented Jul 28, 2011 at 19:57
  • @ TheGeek YouNeed, since AppB will be used/shared by many other web applications, that's why I'm building it as a stand alone web app. Commented Jul 28, 2011 at 19:58

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way implement this with a web application project instead of a class library project? My guess is probably NO since Page class is probably way down the pipe already.
@myself, the answer is YES. I can add a Generic Handler (ashx) page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.