[英]'IApplicationBuilder' does not contain a definition for 'HttpContext'
I am trying to configure the handling of certain HTTP Response Status Codes in the middleware of my ASP.NET Core 2.2 MVC app using this example code from Microsoft docs :我正在尝试使用Microsoft 文档中的此示例代码在我的 ASP.NET Core 2.2 MVC 应用程序的中间件中配置某些 HTTP 响应状态代码的处理:
app.UseStatusCodePages(async context =>
{
context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(
"Status code page, status code: " +
context.HttpContext.Response.StatusCode);
});
But it displays an error for HttpContext
saying但它显示
HttpContext
的错误说
'IApplicationBuilder' does not contain a definition for 'HttpContext' and no accessible extension method 'HttpContext' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
“IApplicationBuilder”不包含“HttpContext”的定义,并且找不到接受“IApplicationBuilder”类型的第一个参数的可访问扩展方法“HttpContext”(您是否缺少 using 指令或程序集引用?)
I see that context
is of type Microsoft.AspNetCore.Diagnostics.StatusCodeContext which has a HttpContext property .我看到
context
是Microsoft.AspNetCore.Diagnostics.StatusCodeContext类型,它具有HttpContext 属性。 Why is it not recognizing HttpContext?为什么它不识别 HttpContext?
PS I tried installing these NuGet packages to no avail: PS我尝试安装这些NuGet包无济于事:
Microsoft.AspNetCore.Diagnostics
Microsoft.AspNetCore.Diagnostics.Abstractions
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.Http.Abstractions
Microsoft.AspNetCore.Http.Extensions
Microsoft.AspNetCore.Http.Features
Discovered the issue ... it's a bit odd: When I check Intellisense on the first instance of HttpContext
it doesn't offer any suggestions for using
statements, but when I do it on any of the other instances it suggests adding a reference to Microsoft.AspNetCore.Http
, which fixes it.发现了这个问题……有点奇怪:当我在
HttpContext
的第一个实例上检查 Intellisense 时,它没有提供任何using
语句的建议,但是当我在任何其他实例上执行它时,它建议添加对Microsoft.AspNetCore.Http
的引用Microsoft.AspNetCore.Http
,修复它。
I'm not sure why it's not finding that suggesting when I check the first HttpContext
.我不确定为什么当我检查第一个
HttpContext
时没有发现该提示。
You need to return a Task
from your lambda expression for the compiler to recognize the correct overload.您需要从 lambda 表达式返回一个
Task
以便编译器识别正确的重载。
You are trying to use this overload:您正在尝试使用此重载:
UseStatusCodePages(IApplicationBuilder, Func<StatusCodeContext,Task>)
But since your lambda expression doesn't return a Task
, the compiler is using this overload:但是由于您的 lambda 表达式不返回
Task
,编译器正在使用此重载:
UseStatusCodePages(IApplicationBuilder, Action<IApplicationBuilder>)
Consequently, your context
variable is actually referring to an instance of IApplicationBuilder
, not StatusCodeContext
.因此,您的
context
变量实际上是指IApplicationBuilder
的实例,而不是StatusCodeContext
。
Returning the Task
from WriteAsync
should do the trick:从
WriteAsync
返回Task
应该可以解决问题:
app.UseStatusCodePages(context =>
{
context.HttpContext.Response.ContentType = "text/plain";
return context.HttpContext.Response.WriteAsync(
"Status code page, status code: " +
context.HttpContext.Response.StatusCode);
});
You are missing some using statements.您缺少一些 using 语句。 Sometimes IntelliSense doesn't quite pick up on where you want to go (statement is probably ambiguous).
有时 IntelliSense 并不能完全了解您想去的地方(声明可能含糊不清)。 You could be more specific and code it like this:
您可以更具体并像这样编码:
async (StatusCodeContext ctx) => ...
Which, in your case would most likely let IntelliSense suggest some using statements that you need, after which you can replace with:在您的情况下,最有可能让 IntelliSense 建议您需要的一些 using 语句,之后您可以替换为:
async ctx => ...
again, and it should probably work.再次,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.