无法访问 .net 标准中的 httpcontext 扩展方法 [英] Can't access httpcontext extension methods in .net standard

查看:34
本文介绍了无法访问 .net 标准中的 httpcontext 扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 .NET Standard 库,我想在其中编写 .NET Core 中间件.

I have this .NET Standard library where I want to write a .NET Core middleware.

里面我想做的:

Endpoint endpoint = httpContext.GetEndpoint();

无法解析GetEndpoint() 扩展方法.

我引用了 Microsoft.AspNetCore.Http 并且我同时拥有 Microsoft.AspNetCore.Http.AbstractionsMicrosoft.AspNetCore.Mvc.Core 添加到项目中的包.

I have referenced Microsoft.AspNetCore.Http and I have both Microsoft.AspNetCore.Http.Abstractions and Microsoft.AspNetCore.Mvc.Core packages added to the project.

有没有办法解决这个问题,我是否遗漏了什么?

Is there a solution to this, am I missing something?

推荐答案

我假设您正在为 ASP.NET Core 3.1 编写中间件,因为您包含了asp.net-core-3.1"标签.

I assume you're writing a middleware for ASP.NET Core 3.1 since you included the "asp.net-core-3.1" tag.

要使用该扩展,您需要针对 netcoreapp3.* 而不是 netstandard2.*:

To use that extension, you need to target netcoreapp3.* instead of netstandard2.*:

<TargetFramework>netcoreapp3.1</TargetFramework>

(您可以在 文档 页面)

您还需要:

  1. 使用 Microsoft.NET.Sdk.Web MSBuild SDK:
  1. use the Microsoft.NET.Sdk.Web MSBuild SDK:
<Project Sdk="Microsoft.NET.Sdk.Web">

  • 或添加框架参考:

  • or add the framework reference:

    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    

  • 参考:在类库中使用 ASP.NET Core API

    这篇关于无法访问 .net 标准中的 httpcontext 扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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