Kenneth J
Kenneth J

Reputation: 4896

Returning a string from a http GET using asp.net web forms project

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?

Upvotes: 1

Views: 1054

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038920

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>

Upvotes: 3

Related Questions