C# Server.Transfer() 对比 服务器.执行()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9261427/
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 06:36:37  来源:igfitidea点击:

Server.Transfer() Vs. Server.Execute()

c#asp.net

提问by Milan Mendpara

im having confusion with which one is better or effect for request of calling page(first page) and caller page (new page) ...

我对呼叫页面(第一页)和呼叫者页面(新页面)的请求哪个更好或效果感到困惑......

i notice that In both the cases, the URL in the browser remains the first page URL (doesn't refresh to the new page URL) as the browser isn't requested to do so.

我注意到在这两种情况下,浏览器中的 URL 仍然是第一页 URL(不会刷新到新页面 URL),因为浏览器没有被要求这样做。

any comments appreciable ....

任何评论可观....

采纳答案by Pranay Rana

Original at : Difference between Server.Transfer and Server.Execute

原文在:Server.Transfer 和 Server.Execute 的区别

Both Server.Transfer and Server.Execute were introduced in Classic ASP 3.0 (and still work in ASP.NET).

Server.Transfer 和 Server.Execute 都是在 Classic ASP 3.0 中引入的(并且仍然在 ASP.NET 中工作)。

When Server.Execute is used, a URL is passed to it as a parameter and the control moves to this new page. Execution of code happens on the new page. Once code execution gets over, the control returns to the initial page, just after where it was called. However, in the case of Server.Transfer, it works very much the same, the difference being the execution stops at the new page itself (means the control isn't returned to the calling page).

当使用 Server.Execute 时,一个 URL 作为参数传递给它,并且控件移动到这个新页面。代码的执行发生在新页面上。一旦代码执行结束,控件就会返回到初始页面,就在它被调用的位置之后。但是,在 Server.Transfer 的情况下,它的工作原理非常相似,不同之处在于执行在新页面本身停止(意味着控件不会返回到调用页面)。

In both the cases, the URL in the browser remains the first page URL (doesn't refresh to the new page URL) as the browser isn't requested to do so.

在这两种情况下,浏览器中的 URL 仍然是第一页 URL(不会刷新到新页面 URL),因为浏览器没有被要求这样做。

回答by da_jokker

I know this is old but it came up as the 1st or 2nd hit when I searched on google. I did some testing and wanted to post the results.

我知道这是旧的,但是当我在谷歌上搜索时,它是第 1 次或第 2 次命中的。我做了一些测试并想发布结果。

I created a website with 2 pages. Page Load on the 1st page contained the code..

我创建了一个有 2 页的网站。第一页上的页面加载包含代码..

try {
    //Response.Redirect("~/WebForm2.aspx");
    //Server.Transfer("~/WebForm2.aspx");
    //Server.Execute("~/WebForm2.aspx");
    //Server.TransferRequest("~/WebForm2.aspx");

    string strTry = "Try";
} catch (Exception ) {
    string strCatch = "Catch";
} finally {
    string strFinally = "Finally";
}

The sequence of what it did for each is what was really interesting...

它为每个人所做的事情的顺序是真正有趣的......

Command            Sequence               
Redirect           Call, Catch (ThreadAbortException), Finally, Load Page 2
Transfer           Call, Load Page 2, Catch (ThreadAbortException), Finally
Execute            Call, Load Page 2, Try (continues), Finally
TransferRequest    Call, Try (continues), Finally, Load Page 2

.. So it may help to know what order you like things to occur in.

.. 因此,了解您喜欢事物发生的顺序可能会有所帮助。

Personally I like the idea of the current code finishing, BEFORE the next page's code starts. So either Redirect or TransferRequest, although with the latter, you may have to add a "return" just below your call if you really intended for it not to execute the rest of the try block.

我个人喜欢在下一页代码开始之前完成当前代码的想法。因此,无论是 Redirect 还是 TransferRequest,尽管对于后者,如果您真的希望它不执行 try 块的其余部分,则您可能必须在调用下方添加“返回”。

回答by Sai Saran

I think this may help you

我想这可能对你有帮助

Server.Transfer

服务器.传输

Server.Transfer is used to transfer control from one page to another page. Control execute code on another page but control doesn't return back to previous page. In this case URL doesn't change in the browser. it can not navigate to outside pages of the application.

Server.Transfer 用于将控制从一个页面转移到另一个页面。控件在另一个页面上执行代码,但控件不返回上一页。在这种情况下,URL 在浏览器中不会改变。它无法导航到应用程序的外部页面。

Syntax :

句法 :

Server.Transfer("URL of another page");

It can access previous page control on new page using following syntax.

它可以使用以下语法访问新页面上的上一页控件。

PreviousPage.FindControl("name of the control")

It conserves server resources by avoiding that extra round-trip.

它通过避免额外的往返来节省服务器资源。

Server.Execute

服务器.执行

Server.Execute is used to transfer control from one page to another page. Control execute code on another page and return back to previous page. In this case URL doesn't change in the browser. it can not navigate to outside pages of the application.

Server.Execute 用于将控制从一个页面转移到另一个页面。控件在另一页上执行代码并返回上一页。在这种情况下,URL 在浏览器中不会改变。它无法导航到应用程序的外部页面。

Syntax :

句法 :

Server.Execute("URL of another page");

It can access previous page control on new page using following syntax.

它可以使用以下语法访问新页面上的上一页控件。

PreviousPage.FindControl("name of the control")

It conserves server resources by avoiding that extra round-trip.

它通过避免额外的往返来节省服务器资源。