从代码隐藏更改asp.net页面的url

发布于 2024-09-24 14:40:25 字数 157 浏览 0 评论 0原文

我试图通过单击按钮将当前 aspx 页面的 url 更改为其他 url。使用Request.Url.AbsoluteUri我可以获取url,但是可以修改吗?如果是,它将采取什么行动,我的意思是这是一个新的请求还是一个回发。

请告诉我您的看法。

谢谢, 梅胡尔·马克瓦纳

I am trying to change change the url of current aspx page to the other url on a click of button. Using Request.Url.AbsoluteUri i can get the url but is it possible to modify?. If yes, what actions will it take i mean will it be a new request or a post back.

Please let me know your views.

Thanks,
Mehul makwana

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

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

发布评论

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

评论(4

我偏爱纯白色 2024-10-01 14:40:25

不,无法从代码隐藏中修改当前页面的 URL。

当代码运行时,新的请求或回发已经在进行中,因此一旦新的响应完成,当前页面将不再存在。

当当前正在创建的页面在浏览器中加载时,将使用它的 URL 而不是当前页面的 URL。该 URL 在请求之前就已决定,因此您也无法更改。

您可以使用 Response.Redirect 方法将重定向页面返回到浏览器,其中包含您想要的 URL。然后,浏览器将向服务器发出另一个请求,以获取具有该 URL 的页面。

如果您想在不使用重定向的情况下更改页面的 URL,那么在代码隐藏中执行此操作就太晚了。您必须使用客户端脚本更改按钮的功能,以便它直接请求新 URL,而不进行回发。

No, it's not possible to modify the URL of the current page from code behind.

When the code runs, a new request or postback is already in progress, so the current page will not exist any more once the new response is complete.

When the page that is currently being created loads in the browser, it's URL will be used instead of the URL of the current page. This URL has already been decided before the request, so you can't change that either.

What you can do is to use the Response.Redirect method to return a redirection page to the browser with the URL that you want. The browser will then make another request to the server to get the page with that URL.

If you want to change the URL of the page to get without using a redirect, doing it in code behind is too late. You have to change what the button does using client script, so that it requests the new URL directly without doing a postback.

不如归去 2024-10-01 14:40:25

您可以使用 Response.Redirect() 或 Server.Transfer() ,尽管我会使用前者而不是后者。

You can use Response.Redirect() or Server.Transfer(), although I'd use the former rather than the latter.

百善笑为先 2024-10-01 14:40:25

考虑使用 jQuery 或类似的库 - 它将允许您修改页面的 DOM。如果只能在服务器上生成新 URL,请考虑向服务器发送 ajax 请求。 Web 方法将返回一些自定义对象,其中包含新的 URL。您将使用 Web 方法返回的数据,并使用 jQuery 或任何类似的库替换它。

Consider using jQuery or similar library - it will allow you to modify DOM of the page. If you can generate new URL only on a server, than consider sending an ajax request to a server. Web method will return you some custom object which will contain a new URL. You'll use data returned by a web method and replace it using a jQuery or any similar library.

执手闯天涯 2024-10-01 14:40:25

这将是一个新请求

您可以尝试使用 Response.Redirect 或 Server.Transfer

it will be a new request .

You can try with Response.Redirect or Server.Transfer

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