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

发布于 2024-09-06 01:10:14 字数 1897 浏览 2 评论 0原文

当我使用以下代码发布页面时,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 (“嘿”)可以在父页面中获取

 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事件中没有任何代码。?

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

编辑:“Hey”将来自 CrossPage.aspx。 当我使用表单操作发布时,我需要将其写回父页面

,在处理 CrossPage.aspx 中的 Page_Load() 事件后,URL 指向“http://localhost:2518/Web/CrossPage.aspx" 这意味着应用程序仍在 CrossPage.aspx 中,并且没有移动到父页面。

When i POST the page using the following code, the Response.write("Hey") doesn't write the content ("Hey") to the parent page

<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>

But When i use following code , and POST the data, the Response.write("Hey") can be obtained in the parent page

 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();
    }

In the CrossPage.aspx i have the following code

 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");
    }

I don't have any code in the Page_Load event of parent page.?

What could be the cause? I need to write the "hey" to the Parent page using the first scenario. Both the application are of different domain.

Edit: "Hey" would be from the CrossPage.aspx. I need to write this back to the Parent Page

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我是男神闪亮亮 2024-09-13 01:10:14

你几乎自己就找到了原因:

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

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

听起来您正在尝试进行某种异步文件上传。尝试查找 AJAX 文件上传示例,如下所示:如何上传异步文件?

You almost hit on the reason yourself:

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.

Your form takes the user to CrossPage.aspx, so the parent page is gone, now the previous page in the user's history.

It sounds like you are trying to do some sort of asynchronous file upload. Try looking for AJAX file upload examples, something like this: How can I upload files asynchronously?

昨迟人 2024-09-13 01:10:14

可能是因为您的代码位于
if (!Page.IsPostBack) 块?仅当页面未在回发时加载时才会执行此代码。

(HttpWebResponse)requestToSender.GetResponse(); 将触发 GET 请求,这就是为什么当您使用该代码调用 Crosspage.aspx 时您的代码可以正常工作。

Probably it is because you have the code in an
if (!Page.IsPostBack) block? this code will be executed only the page is not loaded on a post-back.

(HttpWebResponse)requestToSender.GetResponse(); will trigger a GET request, that's why your code is working when you call Crosspage.aspx using that code.

被你宠の有点坏 2024-09-13 01:10:14

您将该页面视为一项服务。 EG 从 ParentPage.aspx 开始 >将数据传递给ServicePage.aspx进行处理>将响应写回 ParentPage.aspx 以供显示。

您可以通过将职责传递回服务器来使其与 C# 一起使用,在跨页面边界时可以轻松维护状态。当您尝试不使用 C# 来解决问题时,事情就没那么简单了。这不是 Winform 应用程序。正如 NimsDotNet 指出的,将方法更改为“get”将使您更接近,但您将被重定向到 CrossPage.aspx 并丢失调用页面。

你说你们在“不同的领域”。我认为你的意思是你使用两个不同的 IIS 服务器。 C# 解决方案在这种情况下应该仍然有效,正如您所展示的那样。只需添加 fromSender 对象的 Response.Write() 即可。您告诉我们的一切都表明这在技术上是不可能的。不过,如果您想要客户端解决方案,您可以使用 javascript 发出 get 请求而无需重定向。

这篇帖子向您展示了如何使用 JQuery 发出 get 请求。

You are treating the page like a service. E.G. Start at ParentPage.aspx > pass data to ServicePage.aspx for processing > write response back to ParentPage.aspx for display.

You got it to work with C# by passing the duty back to the server, where state can easily be maintained while crossing page boundries. It's not so simple when you try to solve the problem without C#. This isn't a Winform app. As NimsDotNet pointed out, changing the method to "get" will get you closer, but you will get redirected to CrossPage.aspx and lose the calling page.

You said you are on "different domains". By this I think you mean your using two different IIS servers. The C# solution should still work in this scenerio, just as you've shown. Just add a Response.Write() of the fromSender object. There's nothing you've told us that makes this not technically possible. Still, if you want a client side solution you could use javascript to make the get request without getting redirected.

This post shows you how to make a get request with JQuery.

っ左 2024-09-13 01:10:14

我说你的 aFile.SaveAs(Server.MapPath(".") + "/".... 抛出异常。尝试将其注释掉并测试它。

更新:

我猜它可以从 HttpWebRequest 工作,因为有没有文件被发布,因此文件循环被跳过。当从 HTML 发布时,你有一个文件输入,所以你的文件循环被使用并导致保存逻辑被执行,这让我认为这是你的保存逻辑

。 ,我认为您有一个 try catch 语句包含了所有捕获异常的内容,所以您不知道出了什么问题,如果是这种情况,请不要这样做。很少想捕获异常

快速浏览一下您的保存逻辑后,将"/"替换为@"\"。 .MapPath(".") 使用反斜杠而不是正斜杠返回路径。

I say your aFile.SaveAs(Server.MapPath(".") + "/".... is throwing an exception. try commenting it out and testing it.

UPDATE:

I'm guessing it works from a HttpWebRequest because there is no file being posted therefore the file loop is skipped. when posting from the HTML you have a file input so your file loop is getting used and resulting in the save logic being executed. so again, that leads me to think it's your save logic

Also,I think you have a try catch statement wrapped around all this that is catching exception so u have no idea what's going wrong. If that's the case, never do that. You rarely ever want to catch exception.

After quick glance at ur save logic, replace "/" with @"\". Server.MapPath(".") returns the path using back slashes not forward slashes.

悍妇囚夫 2024-09-13 01:10:14

我建议将 CrossPage.aspx 更改为 .ashx HttpHandler。 Page.IsPostBack 可能无法正常工作,因为它需要 ViewState 和其他隐藏的 ASP.net 表单字段来告诉它这是从 ASP 表单回发。您也不需要经历 ASP.net Webforms 所经历的整个页面生命周期功能。

I would suggest changing CrossPage.aspx into an .ashx HttpHandler. The Page.IsPostBack may not be working correctly because it is expecting ViewState and other hidden ASP.net form fields that tell it that it's a post back from an ASP form. You also don't need to go through the whole Page life cycle functionality that ASP.net Webforms goes through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文