How to respond without html in asp.net

Simple question.

Ok, I have a message on my page and you need to reply in one line.

in php, you can just do something like this:

<?php die ("test"); 

then you can host this page on a web server and access it as follows:

 localhost/test.php 

so i need to do the same in c #.

When I try to answer:

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

I get: "<html><head><style type="text/css"></style></head><body>test</body></html>" as an answer.

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

I know that I probably missed some basic knowledge, but I can not find anything on the Internet.

+6
source share
4 answers

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

Also, make sure you compile your page.

Another suggestion is to use the Generic Handler (i.e. *.ashx ), they do not use the typical webforms life cycle and can better match what you are doing.

+5
source

You can clear the previous response buffer and write 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 
+10
source

I think you are looking for:

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

For me, this only generates the actual text in response.write (); expression. I am downloading the complete code for clarity.

Visual Studio: 2010

Code for:

 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> 

EXIT and HTML Source:

I CAN ONLY WATCH IT TO DO NOT INCLUDE ANOTHER HTML TAG

I get the desired result. I tried this code with a master page and got the same result.

Please make sure your AutoEventWireup = "true" if I return this value and then change the SOURCE HTML 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> 
+2
source

Source: https://habr.com/ru/post/914688/


All Articles