当前上下文中不存在名称 Response

The name Response does not exist in the current context

我正在使用 .Net Core 创建 Blazor Web 应用程序并尝试将令牌保存在 cookie 中。但是我得到错误:

The name Response does not exist in the currect context

在我见过的所有例子中,人们都没有遇到这个问题。我必须做什么?

@functions {
    async Task Submit()
    {
        using (var client = new HttpClient())
        {
            User user = new User(state.user.Username, state.user.Password);
            var response = await client.PostAsJsonAsync("api/adimin/token", user);
            var token = await response.Content.ReadAsAsync<string>();

            var option = new CookieOptions();
            option.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Append(setting, token, option);
        }
    }
}

试试这个。

使用system.web

然后写 HttpContext.Current.Response

我知道为什么 Response 在当前上下文中不存在。 实际上这里没有当前上下文。所以这里也没有回应。您的 "current context" 这里是一个 Blazor 组件:"code behind".

  1. 您应该提供问题中的所有相关代码。
  2. 专注于您遇到的一个问题。

我建议您从 Microsoft doc 开始学习 Blazor,当您无法解决问题时,请来这里寻求帮助。 从这里开始:https://docs.microsoft.com/en-us/aspnet/core/blazor/?view=aspnetcore-3.0

希望这对您有所帮助...

这对我很有效:-

Replace 响应 HttpContext.Current.Response