4

I have a php code which I have converted to asp.net code. The PHP code simply echoes a response which a client reads and interpretes, however in asp.net, the generated output is forced to be in html format -- which is precisely because I'm using asp.net labels to print the output.

Is there a way I can achieve the same thing as the echo in php or is there a very lightweight code that can help me parse the html text properly?

EDIT:

What I'm trying to do is like

//get post data

echo "Some stuff"

My current testing aspx file is:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="grabber.aspx.cs" Inherits="qProcessor.grabber" %>

and the code behind has just one method:

    protected void Page_Load(object sender, EventArgs e)
    {
        //this.Response.Write("Welcome!");
    }

Thanks.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • Can you show us what you have for ASP.net code? – RQDQ Apr 25 '12 at 16:12
  • Possible duplicate of [What is the ASP.Net equivalent to PHP's Echo?](http://stackoverflow.com/questions/2977675/what-is-the-asp-net-equivalent-to-phps-echo) – Naman Jul 05 '16 at 19:38

5 Answers5

15

The one-for-one equivalent would be Response.Write:

Response.Write("some text");

That said, ASP .NET and PHP are very different frameworks. With ASP .NET (including the MVC framework) there is rarely a need to write directly to the response stream in this manner.

One such case would be if you wanted to return a very lightweight response. You could do something like this:

Response.ContentType = "text/xml";
Response.Write("<root someAttribute = 'value!' />");

Any method other than using Response directly can (and probably will) alter the output. So in short - if you want to just dump raw data into the HttpResponse, you'll want to use Response.Write().

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • I keep getting this error when I try this: "Object reference not set to an instance of an object." What could be the problem and how can I solve it? Thanks. – Chibueze Opata Apr 25 '12 at 16:24
  • 1
    @ChibuezeOpata Hard to say without seeing your code sample. Probably best to make it another question. If you do, include the relevant code and the exact error message you're getting. – Yuck Apr 25 '12 at 16:26
  • Thanks for your consice and explanatory answer, do you know what could be the cause though? Check the question update for code... – Chibueze Opata Apr 25 '12 at 16:35
  • @ChibuezeOpata Given your example you really shouldn't be using `Response.Write` in the first place. [Here](http://stackoverflow.com/a/10319985/1159478) I show you how ASP expects you to solve this problem. – Servy Apr 25 '12 at 16:37
  • @Yuck I don't know what the heck happened but I just came back from somewhere and it's working fine now. Yes indeed, I'm looking for the lightest response possible as it's a mobile stuff and low bandwidth is heavy consideration. Thanks! – Chibueze Opata Apr 25 '12 at 19:46
4

You can use Response.Write("");

or in your .aspx page use <%="string"%>

Zaki
  • 5,540
  • 7
  • 54
  • 91
  • 2
    And with .NET 4.0 `<%:%>` which adds HTML encoding. – Oded Apr 25 '12 at 16:14
  • Those tags don't write directly to the response stream, though. It's very similar to but not exactly the same as `echo` and `Response.Write()`. Especially with `<%: ... %>` which will do smart HTML encoding before pumping the value into the response. – Yuck Apr 25 '12 at 16:17
2

You can write any text you want to the client:

Response.Write(yourString);
RQDQ
  • 15,461
  • 2
  • 32
  • 59
1

As mentioned by Yuck you really don't need to use Response.Write (which is the direct port of echo) in ASP most of the time. Given your example, you probably want to do something like this:

protected void Page_Load(object sender, EventArgs e)
{

    this.Controls.Add(new LiteralControl(Server.HTMLEncode("<h1>Welcome!</h1>")));
    //will actually print <h1>Welcome!</h1>, rather than Welcome! that's bolded/centered/etc.
}

Or you could even add the literal control, label, etc. to the markup, and then just set the Text property in the code behind. That's the standard approach for solving this issue in an ASP environment.

Community
  • 1
  • 1
Servy
  • 202,030
  • 26
  • 332
  • 449
  • that's what I'm doing but the response ends up having output in html format. – Chibueze Opata Apr 25 '12 at 16:41
  • What *should* the response type be? I added an HTMLEncode method do that you could, for example, write out tags and have them displayed as text rather than as HTML. – Servy Apr 25 '12 at 16:52
  • Thanks for your efforts, I figured out the problem was some computerized misbehaviour but it's all working fine now. Thanks. – Chibueze Opata Apr 25 '12 at 19:47
0

You can use Response.Write() for that:

Response.Write("your text here");
German Latorre
  • 10,058
  • 14
  • 48
  • 59