Question

I am trying to dynamically generate JavaScript with just a URL get request.
I have accomplished this with asp.net MVC by just returning a string from the action and writing the script tag like this.

<script type='text/javascript' src='script/get/123'></script>

The problem is I need to accomplish the same type of dynamically generated script from a asp.net web forms project.

How would I return dynamiccally generated string with a GET request to a page (or web service) in a asp.net Web Forms project?

Was it helpful?

Solution

You could write a generic handler:

public class CustomJsHandler : System.Web.IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/javascript";
        context.Response.Write("alert('Hello world');");
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}

and then specify the address of this handler:

<script type="text/javascript" src="/customjshandler.ashx"></script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top