发布到Azure时TempData无法正常工作 [英] TempData not working when published to Azure

查看:162
本文介绍了发布到Azure时TempData无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站正在玩,以获取Razor Pages的困扰.我有一个奇怪的情况,我不确定正在发生什么或如何解决.我正在使用[TempData]在重定向时传递消息.该应用程序可在本地完美运行.一旦发布到Azure,我将添加一个新项目并添加该项目,我被重定向到索引页面,但我从未看到TempData消息.

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.

这是我的索引页面:

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();
        }
    }
}
} 

这是我的创建页面:

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");
    }
}
}

该站点在本地可用...但是不能发布到Azure.

Locally the site works... but not with published to Azure.

本地结果:

感谢您的帮助!

推荐答案

遇到同样的问题.

在azure上打开流式日志,并发现以下消息:

Turned on streaming logs on azure and found the following message:

Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware:Cookie".AspNetCore.Mvc.CookieTempDataProvider"由于同意政策而被禁止

Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware: Cookie '.AspNetCore.Mvc.CookieTempDataProvider' suppressed due to consent policy

结果是,当我在脚手架上放置网站时,我剥离了GDPR Cookie同意代码,该代码在.net Core MVC应用程序中立即可用,该代码将在接受后创建.AspNet.Consent cookie(值为"yes")

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.

创建cookie后,TempData开始工作.

Once that cookie was created, TempData started working.

如果您不受GDPR的约束,还可以通过将CheckConsentNeeded设置为false来更新cookie策略选项,以不检查是否同意.

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;
        });

这篇关于发布到Azure时TempData无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆