6

Easy question perhaps.

Okay, I have a post to my page and need to respond with one string.

in php, you could simply do something like this:

<?php
die ("test");

then you can place this page on a webserver and access it like this:

localhost/test.php

so, I need to do exact same thing in c#.

When I try to respond with:

 protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("test");
        Response.End();
    }

I'm getting: "<html><head><style type="text/css"></style></head><body>test</body></html>" as a response.

How can I make asp.net to just return exact response, without html?

I know that I probably missing some basic knowledge, but cannot find anything online.

1
  • 1
    I'm unable to reproduce your issue; With VS2010 starting a fresh out of the box "ASP.NET Web Application" and using your code produces as expected: only "test".
    – banging
    May 2, 2012 at 2:51

5 Answers 5

12

You can clear the previous response buffer and write your new output.

Response.Clear(); // clear response buffer
Response.Write("test"); // write your new text
Response.End(); // end the response so it is sent to the client
2
  • I didn't downvote, but it doesn't help. Maybe I need to change the way how I'm calling my script? Currently I'm creating a website with both aspx and cs files.
    – user194076
    May 2, 2012 at 2:40
  • Try changing the event you're attaching to - try Page_PreInit instead.
    – lukiffer
    May 2, 2012 at 2:57
6

Make sure in your *.aspx file, at the top you have AutoEventWireup="true", if it's false (or not there?) your Page_Load event handler will not be called.

Also, make sure you compiled your page.

Another suggestion is to use a Generic Handler (ie *.ashx), these do not use the typical webforms lifecycle and might be better suited to what you're doing.

3
  • Let me try ashx and see what will happen.
    – user194076
    May 2, 2012 at 2:41
  • keep in mind you will use context.Response (or whatever the parameter name is) instead of plain Response.
    – Matthew
    May 2, 2012 at 2:43
  • Probably a noob mistake, but my .aspx page had basic skeleton <html> tags that I didn't think to remove. Once these were gone, my response did not include them (imagine that!) and my json string no longer had a parsing error.
    – Adam
    Dec 8, 2016 at 18:20
5

I think you're looking for :

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Write("test");
        Response.End(); 

    }
2
  • Doesn't help. still the same.
    – user194076
    May 2, 2012 at 2:41
  • @user194076 this should be the answer, i also added Response.Clear() before ContentType just to make sure :P
    – Alpha2k
    Dec 9, 2015 at 15:54
3

For me it only Generates actual text in response.write(); statement. I am uploading the complete code for clarity.

Visual Studio: 2010

Code Behind:

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED");
        Response.End();
    }
}

HTML CODE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

OUTPUT & HTML Source:

I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED

I am getting the desired result. I have tried this code with Master-Page also i get the same result.

Please make sure your AutoEventWireup="true" if i turn this false then HTML SOURCE changes to this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZGivF0fgbeE6VebNR51MYSu3yJdsZ9DwEtIPDBVRf4Vy" />
</div>

    <div>

    </div>
    </form>
</body>
</html> 
0

As the previous answer suggests above, you need AutoEventWireup="true" as well as Response.End() in code behind.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.