在 asp.net c# 中是否有等效的 echo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10319531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:21:32  来源:igfitidea点击:

Is there an equivalent of echo in asp.net c#

c#asp.net

提问by Chibueze Opata

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.

我有一个 php 代码,我已将其转换为 asp.net 代码。PHP 代码只是回显客户端读取和解释的响应,但是在 asp.net 中,生成的输出强制为 html 格式——这正是因为我使用 asp.net 标签来打印输出。

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?

有没有办法可以实现与 php 中的 echo 相同的功能,或者是否有非常轻量级的代码可以帮助我正确解析 html 文本?

EDIT:

编辑:

What I'm trying to do is like

我想做的是

//get post data

echo "Some stuff"

//获取帖子数据

回声“一些东西”

My current testing aspx file is:

我当前的测试 aspx 文件

<%@ 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.

谢谢。

采纳答案by Yuck

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

一对一的等价物是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.

也就是说,ASP .NET 和 PHP 是非常不同的框架。使用 ASP .NET(包括 MVC 框架)很少需要以这种方式直接写入响应流。

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 otherthan using Responsedirectly 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().

任何方法除了使用Response直接可以(并且很可能会)改变输出。简而言之 - 如果您只想将原始数据转储HttpResponseResponse.Write().

回答by RQDQ

You can write any text you want to the client:

您可以向客户端写入任何文本:

Response.Write(yourString);

回答by German Latorre

You can use Response.Write()for that:

你可以使用Response.Write()

Response.Write("your text here");

回答by Zaki

You can use Response.Write("");

您可以使用 Response.Write("");

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

或在您的 .aspx 页面中使用 <%="string"%>

回答by Servy

As mentioned by Yuckyou 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:

正如Yuck提到的,Response.Write大多数时候你真的不需要在 ASP 中使用(这是 echo 的直接端口)。鉴于你的例子,你可能想要做这样的事情:

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 Textproperty in the code behind. That's the standard approach for solving this issue in an ASP environment.

或者您甚至可以将文字控件、标签等添加到标记中,然后只需Text在后面的代码中设置属性。这是在 ASP 环境中解决此问题的标准方法。