在Asp.Net Core中使用TempData时无法重定向到操作

血红素

我正在尝试在Asp.Net Core中实现一个简单的事情。在Asp.Net Mvc中,这没什么大不了的。我有一个这样的动作方法

public async Task<IActionResult> Create([Bind("Id,FirstName,LastName,Email,PhoneNo")] Customer customer)
    {
        if (ModelState.IsValid)
        {
            _context.Add(customer);
            await _context.SaveChangesAsync();
            TempData["CustomerDetails"] = customer;
            return RedirectToAction("Registered");
        }
        return View(customer);
    }

public IActionResult Registered()
    {
        Customer customer = (Customer)TempData["CustomerDetails"];
        return View(customer);
    }

起初,我假设TempData默认工作,但后来意识到必须添加和配置它。我在启动时添加了ITempDataProvider。官方文件似乎描述这应该足够了。没用 然后我还配置了它以使用会话

public void ConfigureServices(IServiceCollection services)
{
      services.AddMemoryCache();
      services.AddSession(
            options => options.IdleTimeout= TimeSpan.FromMinutes(30)
            );
      services.AddMvc();
      services.AddSingleton<ITempDataProvider,CookieTempDataProvider>();
}

我在编写app.UseMvc之前,在Startup的Configure方法中与Session相关的以下行。

app.UseSession();

这仍然无法正常工作。发生的事情是,由于不再使用TempData,我没有遇到任何异常,这是我之前错过某些配置时得到的,但是现在create action方法无法重定向到Registered Method。Create方法可以完成所有工作,但RedirectToAction无效。如果我删除了将客户详细信息分配给TempData的行,则RedirectToAction将成功重定向到该操作方法。但是,在这种情况下,注册操作方法显然无法访问CustomerDetails。我想念什么?

血红素

@赢得。你是对的。阅读本文中的免责声明后,我意识到只要想在Asp.net Core中使用TempData,就必须进行序列化和反序列化。

https://andrewlock.net/post-redirect-get-using-tempdata-in-asp-net-core/

我首先尝试使用BinaryFormatter,但发现它也已从.NET Core中删除。然后,我使用NewtonSoft.Json进行序列化和反序列化。

TempData["CustomerDetails"] = JsonConvert.SerializeObject(customer);

public IActionResult Registered()
    {
        Customer customer = JsonConvert.DeserializeObject<Customer>(TempData["CustomerDetails"].ToString());
        return View(customer);
    }

那是我们现在要做的额外工作,但看起来现在是这样。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将TempData null重定向到View-迁移到ASP.NET Core 3之后

在Asp.Net Core中使用POST方法重定向到URL

在 Asp.net Core 中使用 JQuery 重定向页面

ASP.NET Core 3.1 如何重定向到 MicrosoftIdentity 提供的 AccountController 中包含的登录操作

如何在ASP.NET Core WebAPI中重定向到操作?

从POST操作重定向到url的ASP.NET Core不起作用

ASP.Net MVC重定向到“ /”而不是指定的操作

ASP.Net MVC重定向到“ /”而不是指定的操作

重定向到静态文件asp.net core

在Asp.Net Core 2.x中使用查询字符串重定向

使用Okta登录ASP.NET Core后,将用户重定向到默认页面

ASP.NET MVC重定向到操作重定向循环

添加列表并重定向到另一个视图时,Asp.net核心TempData给出500错误

ASP.NET Core 将 http 重定向到 https,但我不想在 localhost 中执行此操作,我也使用不同的端口

ASP.NET Core自定义中间件重定向到操作不起作用

会话过期时,无需在Controller ASP.NET MVC中执行任何操作即可自动重定向到登录页面

在asp.net mvc 3中重定向到操作时,登录会话有时会丢失

在我的新 PC 中使用 Visual Studio 运行我的 ASP.NET MVC 时,我向它发出的每个请求都会自动将其重定向到 Microsoft 登录

更新重定向ASP .net

如何使用ASP.NET Core中的JWT授权重定向到401上的登录页面

使用MVC重定向到ASP.NET Core中的上一页URL-C#

使用 HttpPost 方法提交表单后 ASP.NET Core 重定向到同一页面

路由问题ASP.net MVC 4没有重定向到索引操作

从 Jquery 重定向到控制器 asp.net 中的操作

使用ADFS和ASP.NET MVC时未授权用户将用户重定向到自定义页面

重定向操作方法未收到ASP.NET Core MVC ChallengeResult,AuthenticationProperties参数

当用户未被授权使用 ASP.NET Core 2.2 和 OIDC 时自动重定向到外部权限

从ASP.NET Web API重定向操作

防止在 asp.net Core 5 Razor 页面中重定向到 /Account/Login