ASP.net core 5.0 Razor 页面重定向到新 IP

问题描述 投票:0回答:1

我正在开发基于 Razor Pages 的 Asp.net core 5.0 Web 服务器。 该网站托管一个配置页面,用户可以在其中更改当前服务器 IP,即运行应用程序的服务器的地址。

换句话说,事件链就是这样的:

  1. 服务器唤醒,开始在IP 1上托管
  2. 用户访问IP1/配置页面并进行配置
  3. 这样的配置会导致服务器IP从IP1更改为IP2
  4. 在这种情况下,我想将用户重定向到新页面。

为了实现这一点,我使用以下代码:

if (address != this.Address)
    return RedirectPermanent($@"http://{this.Address}/configuration");

这就是我陷入困境的地方:页面停留在那里,没有响应(因为 IP 已更改) 并且不重定向。

当我运行调试时,我收到此日志:

[15:20:33.254 DBG] [(C:\Projects\Vabw\RI636\SERVER\Pages\Configuration.cshtml.cs) OnPostAsync, Line 493] Previous address: 172.18.1.101 GUI address: 172.18.102.134 CFG address: 172.18.102.134
[15:20:33.299 INF] Executed handler method OnPostAsync, returned result Microsoft.AspNetCore.Mvc.RedirectResult.
[15:20:33.303 VRB] Page Filter: Before executing OnPageHandlerExecuted on filter Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.HandleOptionsRequestsPageFilter.
[15:20:33.307 VRB] Page Filter: After executing OnPageHandlerExecuted on filter Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.HandleOptionsRequestsPageFilter.
[15:20:33.311 VRB] Page Filter: After executing OnPageHandlerExecutionAsync on filter Microsoft.AspNetCore.Mvc.Filters.PageHandlerPageFilter.
[15:20:33.313 VRB] Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.316 VRB] Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.319 VRB] Before executing action result Microsoft.AspNetCore.Mvc.RedirectResult.
[15:20:33.338 INF] Executing RedirectResult, redirecting to http://172.18.102.134/configuration.
[15:20:33.341 VRB] After executing action result Microsoft.AspNetCore.Mvc.RedirectResult.
[15:20:33.343 VRB] Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.348 VRB] Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.349 VRB] Resource Filter: Before executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.353 VRB] Resource Filter: After executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.SaveTempDataFilter.
[15:20:33.356 INF] Executed page /Configuration in 5971.6781ms
[15:20:33.359 INF] Executed endpoint '/Configuration'
[15:20:33.368 DBG] Connection id "0HN3DPSOMK7JP" completed keep alive response.
[15:20:33.376 INF] Request finished HTTP/1.1 POST http://172.18.1.101/configuration/80085 application/x-www-form-urlencoded 601 - 301 0 - 6099.0317ms

对我来说,看起来重定向调用已经完成,但随后原始的后调用取代并阻止重定向操作发生..?

c# asp.net-core web-applications razor-pages
1个回答
0
投票

我根本没有使用重定向就解决了我的问题。

问题在于,在执行OnPost调用期间,IP服务器会在客户端不知情的情况下发生变化。那么任何重定向都将源自一个全新的源,其效果是客户端保持静止。

因此,我没有主动将客户端重定向到新 IP,而是简单地保存了当前配置,如果 IP 发生更改,我将恢复到旧配置,并渲染页面并显示一条消息,告诉用户转到新地址。与此同时,

Task.Run(() => UpdateIp())
例程将按照经验设置的 2.5 秒延迟将 IP 更新为新 IP。

总之,我最初问题的答案是“这是不可能的,因为连接引用了不存在的服务器”,但我设法找到了一种解决方法,可以让用户访问新页面。

© www.soinside.com 2019 - 2024. All rights reserved.