C# ConstraintMap引发的ASP.NET Core 3.0 Web API InvalidOperationException

C# ConstraintMap引发的ASP.NET Core 3.0 Web API InvalidOperationException,c#,asp.net-core,C#,Asp.net Core,我正在使用: .net核心SDK v3.0.100 asp.net核心3.0 VS代码 设置 我创建了am ASP.NET Core 3.0 Web API。我已将单个操作添加到我唯一的控制器 我已经阅读了关于升级到asp.net core 3.0的说明,以确保我已经对此版本的框架进行了相关更改。我已经在下面详细介绍了相关的代码块 通用主机生成器(来自程序.cs) 公共静态IHost BuildHost(字符串[]args)=> Host.CreateDefaultBuilder(args)

我正在使用:

  • .net核心SDK v3.0.100
  • asp.net核心3.0
  • VS代码
设置

我创建了am ASP.NET Core 3.0 Web API。我已将单个操作添加到我唯一的控制器

我已经阅读了关于升级到asp.net core 3.0的说明,以确保我已经对此版本的框架进行了相关更改。我已经在下面详细介绍了相关的代码块

通用主机生成器(来自
程序.cs

公共静态IHost BuildHost(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.useserlog()
.ConfigureWebHostDefaults(webHostBuilder=>
{
网络主机生成器
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup();
})
.Build();
Startup.cs

公共类启动
{
public void配置服务(IServiceCollection服务)
{
services.AddControllers();
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseRouting();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
});
}
}
MyController.cs

[路由(“api/v1/[控制器]”)]
[ApiController]
公共类MyController
:控制器基
{
[HttpGet(“/uri”)]
[产品响应类型(StatusCodes.Status200OK)]
[产品响应类型(StatusCodes.Status204NoContent)]
公共异步任务GetResourcesAsync()
{
//异步地做一些事情
}
}
问题

我可以通过发布
dotnet run--project
来运行应用程序。当它运行时,输出表示它正在监听
http://localhost:5000

如果我通过运行

curlhttp://localhost:5000/api/v1/my/uri

抛出以下错误。我在谷歌上搜索了一下,我能找到的只是一些关于

但我不明白为什么我需要自定义路由约束,因为我认为我没有向路由添加自定义约束

非常感谢您的帮助

错误

[20:45:08 ERR] An unhandled exception has occurred while executing the request.
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)
[20:45:08 INF] Request finished in 120.1815ms 500 text/plain
[20:45:08 ERR]执行请求时发生未经处理的异常。
System.InvalidOperationException:无法将约束引用“string”解析为类型。使用“Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap”注册约束类型。
在Microsoft.AspNetCore.Routing.DefaultParameterPolicyFactory.Create(RoutePatternPareterPart参数,字符串inlineText)中
在Microsoft.AspNetCore.Routing.ParameterPolicyFactory.Create(RoutePatternPareterPart参数,RoutePatternPareterPolicyReference)
位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.CreateCandidate(端点,Int32分数)
位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.CreateCandidates(IReadOnlyList`1端点)
位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]状态,Int32 exitDestination)的Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]状态,Int32 exitDestination),位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.g|转换| 19|0(下一步,c__显示19_0&)
位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]状态,Int32 exitDestination)的Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]状态,Int32 exitDestination)的Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.AddNode(DfaNode节点,DfaState[]states,Int32 exitDestination)的Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.Build()中的状态,Int32 exitDestination)
位于Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.CreateMatcher(IReadOnlyList`1端点)
位于Microsoft.AspNetCore.Routing.DataSourceDependentCache`1.Initialize()
在System.Threading.LazyInitializer.EnsureInitializedCore[T](T&target,Boolean&initialized,Object&syncLock,Func`1 valueFactory)
在System.Threading.LazyInitializer.EnsureInitialized[T](T&target,Boolean&initialized,Object&syncLock,Func`1 valueFactory)
位于Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource数据源,生存期,Func`1 matcherBuilderFactory)
位于Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource数据源)
在Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreSync()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g_|u WaitingMatcher | 8_0(EndpointRoutingMiddleware中间件,HttpContext HttpContext,Task`1 matcherTask)
位于Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)
[20:45:08 INF]请求以120.1815ms 500文本/纯文本完成

1.您应该访问
/uri
而不是
/api/v1/my/uri
2.您应该使用
https://
而不是
http://
3.您将发送一个http
HEAD
请求,而不是http
GET
。但是,您的
GetResourcesAsync()
方法根本不接受HTTP
Head
请求。4.最后,在更正上述错误时,您的代码对我来说运行良好。如果这些步骤不能解决问题,是否有一个最小的演示?是的,对于协议和curl错误,我深表歉意…将制作一个小演示并上传到github repo,因为我遇到了同样的错误。另外,关于第1点。为什么我要访问
/uri
而不是
/api/v1/my/uri
?如果未指定请求,该请求如何映射到相应的控制器?如果希望