简体   繁体   English

“IApplicationBuilder”不包含“HttpContext”的定义

[英]'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 .我看到contextMicrosoft.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.

相关问题 “IApplicationBuilder”不包含“MapAzureSignalR”的定义 - 'IApplicationBuilder' does not contain a definition for 'MapAzureSignalR' IApplicationBuilder 不包含 UseIdentity 的定义 - IApplicationBuilder does not contain a definition for UseIdentity IApplicationBuilder 不包含 UseAzureAppConfiguration 的定义 - IApplicationBuilder does not contain a definition for UseAzureAppConfiguration IApplicationbuilder 不包含 CreatePerOwinContext 的定义 - IApplicationbuilder does not contain definition for CreatePerOwinContext “IApplicationBuilder”不包含“UseWebAssemblyDebugging”的定义 - 'IApplicationBuilder' does not contain a definition for 'UseWebAssemblyDebugging' “IServiceCollection”不包含“AddMvc”的定义,“IApplicationBuilder”不包含“UseStaticFiles”的定义, - 'IServiceCollection' does not contain a definition for 'AddMvc', 'IApplicationBuilder' does not contain a definition for 'UseStaticFiles', HttpContext.GetOwinContext() 不包含 GetOwinContext() 的定义吗? - HttpContext.GetOwinContext() does not contain definition for GetOwinContext()? 网络核心:“ HttpContext”不包含“当前”的定义 - Net Core: 'HttpContext' does not contain a definition for 'Current'` HttpContext不包含Current的定义 - HttpContext doess not contain definition for Current 我使用“HttpContext.Current”,但调试时出现错误“HttpContext”不包含“Current”的定义 - I use “HttpContext.Current” but debug got error “HttpContext' does not contain a definition for 'Current”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM