0

I want to create a web form where I'll perform some background action based on the GET parameter through jQuery and will print 1 or 0. But the problem - ASP.NET is printing lots of HTML by default even when no such code exist in the Markup window

<%@ Page Language="C#"%>
<%
  try
  {
    int cID = int.Parse(Request.QueryString["cid"].ToString());
    //do some job and print 1 or 0
  }
  catch { }
%>

But in the output window I see HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>
5
  • 1
    That markup is the base Html required to define a page, give or take. Why is it a problem? It won't cause anything to be displayed to the end-user except for a blank page. Commented Oct 1, 2012 at 14:43
  • I just need to have defined output not HTMLs :( Commented Oct 1, 2012 at 14:44
  • 2
    It sounds like you're creating a web service, not a web page. Could you create a service and customise the output of that, instead? Commented Oct 1, 2012 at 14:45
  • Actually I was doing it the PHP way (: Will try that, but have never tried using Service Commented Oct 1, 2012 at 14:48
  • @DanPuzey Can you please show me a sample Hello World example ? Commented Oct 1, 2012 at 14:53

1 Answer 1

1

I would recommend you use an ashx (Add New Item > Generic Handler) handler instead of aspx form

inside the process request change the response Mime type to text/plain and simply write the query string value to the response

MyHandler.ashx.cs:

/// <summary>
/// Summary description for MyHandler
/// </summary>
public class MyHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(context.Request.QueryString["cid"]);
    }

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

getting the value client-side with jQuery:

        $.ajax({
            type: "GET",
            url: "MyHandler.ashx?cid=10",
            contentType: "text/plain",
            dataType: "text",
            success: function (data) {
                alert(data);
            }
        })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.