简体   繁体   English

约束引用“字符串”无法解析为类型。 (netcoreapp3.0)

[英]The constraint reference 'string' could not be resolved to a type. (netcoreapp3.0)

I have got an error.我有一个错误。 This is my Startup class.这是我的启动课。

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
}

public void Configure(IApplicationBuilder app)
{
    app.UseDeveloperExceptionPage();

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Current technology "netcoreapp3.0" and my controller is.当前技术“netcoreapp3.0”和我的控制器是。

[Route("api/[controller]")]
[ApiController]
public class ExampleController : ControllerBase
{
    [HttpGet("request")]
    public ActionResult Index()
    {
        return Ok();
    }
}

And here is my error.这是我的错误。 I couldn't find a solution, I even didn't get what exactly this is.我找不到解决方案,我什至不知道这到底是什么。 So here we are.所以我们在这里。

System.InvalidOperationException: The constraint reference 'string' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'.
   at Microsoft.AspNetCore.Routing.DefaultParameterPolicyFactory.Create(RoutePatternParameterPart parameter, String inlineText)
   at Microsoft.AspNetCore.Routing.ParameterPolicyFactory.Create(RoutePatternParameterPart parameter, RoutePatternParameterPolicyReference reference)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.CreateCandidate(Endpoint endpoint, Int32 score)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.CreateCandidates(IReadOnlyList`1 endpoints)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.<AddNode>g__Transition|19_0(DfaNode next, <>c__DisplayClass19_0& )
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode node, DfaState[] states, Int32 exitDestination)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.Build()
   at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.CreateMatcher(IReadOnlyList`1 endpoints)
   at Microsoft.AspNetCore.Routing.DataSourceDependentCache`1.Initialize()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func`1 matcherBuilderFactory)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task`1 matcherTask)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

EDIT: There is my dependencies.编辑:有我的依赖项。 When I remove these then it worked.当我删除这些然后它工作。

<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc4" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="3.0.24" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />

In case you use something like如果你使用类似的东西

[HttpGet("example/{param1:string}/{param2:Guid}")]

change it to将其更改为

[HttpGet("example/{param1}/{param2:Guid}")]

because ":string" is actually interpreted as a regex-validation-constraint and not a type definition and guess what, everything is reaching the server as string and there is no string-regex-validator :)因为 ":string" 实际上被解释为regex-validation-constraint不是类型定义,猜猜看,一切都以字符串形式到达服务器,并且没有 string-regex-validator :)

I also encountered this recently.我最近也遇到了这种情况。 The fix for me as to use "alpha" as a replacement for the string type:对我来说使用“alpha”作为字符串类型的替代品的修复:

[HttpGet("example/{param1:alpha}")]

This was documented in the Microsoft documentation .这已记录在Microsoft 文档中。

It could be a blank space between the param name and the type.它可以是参数名称和类型之间的空格。

Example:例子:

{param1 :alpha} 🚫 {param1 :alpha} 🚫

{param1:alpha} ✅ {param1:alpha} ✅

在我的情况下,它只是路由参数和数据类型({id:int})之间的一个空格

Ahh, my issue was variable naming mismatch.啊,我的问题是变量命名不匹配。

Not Working不工作

[HttpGet("{id}")]
public async Task<ActionResult<UserDto>> Get(string userId) {
    //some code
}

"userId" should change to "id" “userId”应更改为“id”

Working在职的

[HttpGet("{id}")]
public async Task<ActionResult<UserDto>> Get(string id) {
    //some code
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 约束引用“字符串”无法解析为类型。 键入 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap' - The constraint reference 'string' could not be resolved to a type. type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap' "约束引用“字符串”无法解析为类型" - The constraint reference 'string' could not be resolved to a type 使用 netcoreapp3.0 安装 dotnet-ef 时遇到问题 - Facing Issue while Installing dotnet-ef with netcoreapp3.0 无法为面向 netcoreapp3.0 的 ASP.NET Core WebApp 中的特定 API 控制器启用 CORS - Can't enable CORS for specific API controllers in ASP.NET Core WebApp targeting netcoreapp3.0 为什么 Path.GetFullPath 返回带有额外“bin/Debug/netcoreapp3.0”的地址? - Why Path.GetFullPath returns the address with extra "bin/Debug/netcoreapp3.0"? 在netcoreapp3.0控制台应用程序中,默认情况下会生成myprogram.exe(带扩展名exe)。 它是什么? - In netcoreapp3.0 console application, myprogram.exe (with extension exe) is generated by default. What is it? 将目标从 netcoreapp3.0 更改为 netstandard2.1 时缺少 TableAttribute - TableAttribute is missing when change target from netcoreapp3.0 to netstandard2.1 将base64string转换为Image类型。找不到图片类型? - Converting base64string to Image type. Image type could not be found? 找不到类型或命名空间名称“SerializationFormatter”netcoreapp1.1 - The type or namespace name 'SerializationFormatter' could not be found netcoreapp1.1 主要参考“ Google.Apis”无法解析 - The primary reference “Google.Apis” could not be resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM