I have a site I'm playing with to get the hang of Razor Pages. I have a weird situation I'm unsure what is happening or how to resolve. I'm using [TempData] to pass a message on redirect. The app works perfectly locally. Once published to Azure I add a new item and the item is added, I'm redirected to the index page but I never see the TempData message.
Here is my Index page:
public class IndexModel : PageModel
{
private readonly TheFishRoom_MVC_Core.Data.FishRoomDbContext _context;
public IndexModel(TheFishRoom_MVC_Core.Data.FishRoomDbContext context)
{
_context = context;
}
public IList<Coral> Coral { get; set; }
[TempData]
public string Message { get; set; }
public bool ShowMessage => !string.IsNullOrEmpty(Message);
public async Task OnGetAsync(string searchString)
{
if (!String.IsNullOrEmpty(searchString))
{
Coral = await _context.Corals.Where(s => s.Name.Contains(searchString)).ToListAsync();
}
else
{
Coral = await _context.Corals.ToListAsync();
}
}
}
}
Here is my Create page:
namespace TheFishRoom_MVC_Core.Pages.Corals
{
[Authorize(Roles = "Admin")]
public class CreateModel : PageModel
{
private readonly FishRoomDbContext _context;
public CreateModel(FishRoomDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Coral Coral { get; set; }
[TempData]
public string Message { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Corals.Add(Coral);
await _context.SaveChangesAsync();
Message = "New Coral created successfully!";
return RedirectToPage("./Index");
}
}
}
Locally the site works... but not with published to Azure.
Any help is appreciated!
Ran into the same issue.
Turned on streaming logs on azure and found the following message:
Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware: Cookie '.AspNetCore.Mvc.CookieTempDataProvider' suppressed due to consent policy
Turns out when I was scaffolding my site I stripped the GDPR Cookie Consent code that comes out of the box in .net Core MVC apps that will create the .AspNet.Consent cookie (with a value of "yes") when accepted.
Once that cookie was created, TempData started working.
You can also update the cookie policy options to not check for consent by setting CheckConsentNeeded to false if you are not subject to GDPR.
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
来源:https://stackoverflow.com/questions/50865173/tempdata-not-working-when-published-to-azure