【问题标题】:ASP.NET Core 3.1: POST-REDIRECT-GET: Passing parametersASP.NET Core 3.1:POST-REDIRECT-GET:传递参数
【发布时间】:2021-11-18 23:08:08
【问题描述】:

我正在尝试实现 POST-REDIRECT-GET 技术,其中我将 JSON 字符串发布到我的 Razor OnPost 方法。这反过来又重定向到接受该字符串参数的 OnGet 方法。如何将此输入参数字符串隐藏到我的 OnGet 方法中?

编辑:我尝试使用 ViewData,但我的 OnGet 方法中的值始终为 null,即使我在重定向之前设置了它。

public async Task<IActionResult> OnPostData(string input_JSON)        
{
    TempData["InputJSON"] = input_JSON;
    return RedirectToPage("GetData");
}

public async Task<IActionResult> OnGetGetData()
{
     string tempData = TempData["InputJSON"] as string;
    //do somethig with string;
}

我在我的 javascript 中进行表单发布,当新窗口打开时,我在我的 URL 中看到了 input_string。如何在方法之间传递参数?

【问题讨论】:

    标签: .net-core asp.net-core-3.1


    【解决方案1】:

    https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.2#tempdata-provider-and-session-state-cookies-arent-essential

    根据 Microsoft 的说法,TempData 提供程序和会话状态 cookie 不是必需的。我必须在我的 Startup.cs 文件的 ConfigureServices 中使它成为必不可少的:

    services.Configure<CookieTempDataProviderOptions>(options => {
        options.Cookie.IsEssential = true;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-09
      • 2021-01-26
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2020-07-19
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多