Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond without html in asp.net

Tags:

c#

asp.net

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.

like image 401
user194076 Avatar asked May 02 '12 02:05

user194076


3 Answers

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
like image 163
lukiffer Avatar answered Oct 17 '22 16:10

lukiffer


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.

like image 26
Matthew Avatar answered Oct 17 '22 15:10

Matthew


I think you're looking for :

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

    }
like image 5
tzerb Avatar answered Oct 17 '22 17:10

tzerb