ASP.NET如何返回一个字符串而不是返回页面?

时间:2015-06-02 03:23:38

标签: c# asp.net

这是我想获得回复的asp.net页面

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        B2.Text = Request.QueryString["webget"];
    }
}

使用此代码以表格

发送请求
private void button1_Click(object sender, EventArgs e)
{
    string UrlString = "http://localhost:10694/Default.aspx?webget=more";
    HttpWebRequest req = HttpWebRequest.Create(UrlString) as HttpWebRequest;
    req.Method = "GET";
    string result = "";
    try
    {
        using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }
        }
    }
    catch
    {
        label1.Text = "error";
    }
    label1.Text = result;
}

此代码将下载并显示Default.aspx 但是如果我想从ASP.NET返回一个字符串,而不是下载页面

如何在ASP.NET中执行此操作?

1 个答案:

答案 0 :(得分:3)

你的意思是“Response.Write”功能吗?

https://msdn.microsoft.com/en-us/library/ms525585%28v=vs.90%29.aspx

如果要在响应中返回字符串,请尝试以下操作: 在页面中,写下:

string rtnString = "yourStringHere";
Response.Write(rtnString);
Response.End();