Asp.net core mvc asp.net core 2.1 tempdata为空,带redirecttoaction
我有一个asp.net core 2.1项目,我尝试将TempData与RedirectToAction一起使用,但它总是空的(没有错误) 这是我的ConfigureServices方法Asp.net core mvc asp.net core 2.1 tempdata为空,带redirecttoaction,asp.net-core-mvc,asp.net-core-2.1,Asp.net Core Mvc,Asp.net Core 2.1,我有一个asp.net core 2.1项目,我尝试将TempData与RedirectToAction一起使用,但它总是空的(没有错误) 这是我的ConfigureServices方法 public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { /
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
//services pour l'authentification
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Login";
});
//services pour session
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(20);
});
//configuer port https
services.AddHttpsRedirection(options => options.HttpsPort = 443);
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
ManageDI(services);
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();
}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
//认证服务
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(选项=>
{
options.LoginPath=“/Login”;
});
//服务倾注期
services.AddSession(选项=>{
options.IdleTimeout=TimeSpan.FromMinutes(20);
});
//配置器端口https
services.AddHttpsRedirection(options=>options.HttpsPort=443);
Dapper.DefaultTypeMap.MatchNameswithUnderlines=true;
经理(服务);
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();
}
我的配置方法中有“app.UseSession();”
这是我的密码
[HttpGet]
public async Task< IActionResult> ResetPassword(string query)
{
TempData["test"]= "test";
return RedirectToAction(nameof(Login));
}
[HttpGet]
public IActionResult Login(string returnUrl = null)
{
var b = TempData["test"];
//b is always null when calling ResetPassword action
var model = new Models.Account.LoginModel{
ReturnUrl = returnUrl
};
return View(model);
}
[HttpGet]
公共异步任务ResetPassword(字符串查询)
{
TempData[“测试”]=“测试”;
返回重定向到操作(名称(登录));
}
[HttpGet]
public IActionResult登录(字符串returnUrl=null)
{
var b=临时数据[“测试”];
//调用ResetPassword操作时,b始终为空
var模型=新模型.Account.LoginModel{
ReturnUrl=ReturnUrl
};
返回视图(模型);
}
请问我忘了什么
谢谢根据您提供的代码,还不完全清楚问题出在哪里,但是由于您在
登录
操作中提到重置密码
操作中该值为空,因此我假设您没有正确地保留该值
TempData
只是:临时数据。一旦被访问,它就会被删除。因此,当你在这里设置b
和它的值时,它就消失了。如果您随后尝试在另一个操作中访问它,甚至只是在该操作返回的视图中访问它,那么它现在将为null
如果您需要获取该值,但还要保留该值以备将来使用,则需要使用TempData.Peek
:
var b = TempData.Peek("test");
我终于找到了答案:在Startup.Configure()app.UseCookiePolicy()中;应该在app.UseMVC()之后;