Web应用程序2中的DefaultInlineConstraintResolver错误

Hal*_*yon 131 c# asp.net api iis asp.net-web-api

我正在使用Web API 2,当我在本地方框上使用IIS 7.5向我的API方法发送POST时,我收到以下错误.

The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'.

Line 21: GlobalConfiguration.Configuration.EnsureInitialized();
Run Code Online (Sandbox Code Playgroud)

我的API都不能使用IIS.但是,我能够使用IIS Express在Visual Studio中运行我的API项目并成功对我的登录API进行POST,但是当我尝试向另一个API调用发出GET请求时,我得到约束解析器错误.

为了解决这个问题,我在Visual Studio中创建了一个全新的Web API 2项目,并开始一次一个地将现有API导入到新项目中并运行它们以确保它们正常工作.在这个新项目中使用IIS Express,我得到了与现有API项目相同的结果.

我在这里错过了什么?即使有一个全新的项目,我也无法在没有遇到这个约束解析器问题的情况下发出GET请求.

Kir*_*lla 259

错误意味着在Route中的某个地方,你指定了类似的东西

[Route("SomeRoute/{someparameter:string}")]
Run Code Online (Sandbox Code Playgroud)

如果没有指定其他内容,则不需要"string",因为它是假定的类型.

如错误所示,DefaultInlineConstraintResolverWeb API附带的内容没有调用内联约束string.默认支持的是以下内容:

// Type-specific constraints
{ "bool", typeof(BoolRouteConstraint) },
{ "datetime", typeof(DateTimeRouteConstraint) },
{ "decimal", typeof(DecimalRouteConstraint) },
{ "double", typeof(DoubleRouteConstraint) },
{ "float", typeof(FloatRouteConstraint) },
{ "guid", typeof(GuidRouteConstraint) },
{ "int", typeof(IntRouteConstraint) },
{ "long", typeof(LongRouteConstraint) },

// Length constraints
{ "minlength", typeof(MinLengthRouteConstraint) },
{ "maxlength", typeof(MaxLengthRouteConstraint) },
{ "length", typeof(LengthRouteConstraint) },

// Min/Max value constraints
{ "min", typeof(MinRouteConstraint) },
{ "max", typeof(MaxRouteConstraint) },
{ "range", typeof(RangeRouteConstraint) },

// Regex-based constraints
{ "alpha", typeof(AlphaRouteConstraint) },
{ "regex", typeof(RegexRouteConstraint) }
Run Code Online (Sandbox Code Playgroud)

  • 如果没有指定其他内容,则不需要"string",因为它是假定的类型. (29认同)
  • @AndreasFurster:因为`string`不能应用任何约束. (3认同)
  • 这是有道理的,为什么我看到错误.我的路由属性中有{string:type}.我删除它,它现在正在工作. (2认同)
  • 如果问题是因为路由属性如:{string:type},只需删除'string:' (2认同)

小智 32

还有一件事,如果你不能使用int,bool或任何其他约束,它是关键敏感的,你需要删除任何空格.

//this will work
[Route("goodExample/{number:int}")]
[Route("goodExampleBool/{isQuestion:bool}")]
//this won't work
[Route("badExample/{number : int}")]
[Route("badExampleBool/{isQuestion : bool}")]
Run Code Online (Sandbox Code Playgroud)

  • 你会认为他们在拆分之后和进行比较之前会`trim()`这些......不修剪用作键的字符串是我的主要烦恼,回到我的 FoxPro 时代。 (2认同)

Lat*_*ior 9

当我在路径中的变量名和变量类型之间留下空格时,我也遇到了这个错误,如下所示:

[HttpGet]
[Route("{id: int}", Name = "GetStuff")]
Run Code Online (Sandbox Code Playgroud)

它应该是以下内容:

[HttpGet]
[Route("{id:int}", Name = "GetStuff")]
Run Code Online (Sandbox Code Playgroud)