c# - 为什么 Response.Write 行为在给定场景中会有所不同?

标签 c# asp.net cross-domain response.write

当我使用以下代码发布页面时,Response.write("Hey") 不会将内容("Hey")写入父页面

<form method="post" name="upload" enctype="multipart/form-data"
action="http://localhost:2518/Web/CrossPage.aspx" >
<input type="file" name="filename" />
<input type="submit" value="Upload Data File" name="cmdSubmit" />
</form>

但是当我使用下面的代码,并发布数据时,可以在父页面中获取 Response.write("Hey")

 HttpWebRequest requestToSender = (HttpWebRequest)WebRequest.Create("http://localhost:2518/Web/CrossPage.aspx");
 requestToSender.Method = "POST";
 requestToSender.ContentType = "multipart/form-data";

 HttpWebResponse responseFromSender = (HttpWebResponse)requestToSender.GetResponse();
 string fromSender = string.Empty;

 using (StreamReader responseReader = new StreamReader(responseFromSender.GetResponseStream()))
    {
        fromSender = responseReader.ReadToEnd();
    }

在 CrossPage.aspx 中我有以下代码

 if (!Page.IsPostBack)
    {
        NameValueCollection postPageCollection = Request.Form;

        foreach (string name in postPageCollection.AllKeys)
        {
            Response.Write(name + " " + postPageCollection[name]);
        }

        HttpFileCollection postCollection = Request.Files;
        foreach (string name in postCollection.AllKeys)
        {
            HttpPostedFile aFile = postCollection[name];
            aFile.SaveAs(Server.MapPath(".") + "/" + Path.GetFileName(aFile.FileName));
        }

        Response.Write("Hey");
    }

我在父页面的 Page_Load 事件中没有任何代码?

可能是什么原因?我需要使用第一个场景将“嘿”写到父页面。两个应用程序属于不同的领域。

编辑:“嘿”将来自 CrossPage.aspx。我需要将其写回父页面

当我使用表单操作发布时,在处理 CrossPage.aspx 中的 Page_Load() 事件后,URL 指向“http://localhost:2518/Web/CrossPage.aspx”,这意味着应用程序仍在 CrossPage.aspx 中并且没有移动到父级页面。

最佳答案

你几乎自己找到了原因:

when i post using the form action, after processing the Page_Load() event in CrossPage.aspx, the URL points to "http://localhost:2518/Web/CrossPage.aspx" which means the application is still in the CrossPage.aspx and didn't move to parent page.

您的表单将用户带到 CrossPage.aspx,因此父页面消失了,现在是用户历史记录中的上一页。

听起来您正在尝试进行某种异步文件上传。尝试寻找 AJAX 文件上传示例,如下所示:How can I upload files asynchronously?

关于c# - 为什么 Response.Write 行为在给定场景中会有所不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052945/

相关文章:

c# - 按值从 KeyValuePair 列表中删除重复项

iframe - 谷歌分析显示来自(脚本加载)第 3 方 iFrame 的推荐流量

c# - WPF 应用程序退出代码

c# - 忽略嵌套属性的 "Object reference not set to an instance of an object"错误的最佳方法是什么?

asp.net - 为什么我想将 UnitOfWork 与存储库模式一起使用?

c# - 使用复选框通过 javascript 更改对象可见性?

asp.net - 在 AspNet vNext (MVC 6) 中将 NLog 与 asp-net 布局渲染器一起使用

javascript - Uploadcare 通过 API 删除问题

internet-explorer - 在 HTTPS 和 HTTP 之间传递数据

c# - 以编程方式单击复选框