WebAPI 2 中的 DefaultInlineConstraintResolver 错误 [英] DefaultInlineConstraintResolver Error in WebAPI 2

查看:34
本文介绍了WebAPI 2 中的 DefaultInlineConstraintResolver 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Web API 2,当我在本地机器上使用 IIS 7.5 向我的 API 方法发送 POST 时出现以下错误.

DefaultInlineConstraintResolver"类型的内联约束解析器无法解析以下内联约束:string".第 21 行:GlobalConfiguration.Configuration.EnsureInitialized();

我的 API 都没有使用 IIS 工作.但是,我可以使用 IIS Express 在 Visual Studio 中运行我的 API 项目,并成功向我的登录 API 发出 POST 请求,但是当我尝试向另一个 API 调用发出 GET 请求时,我收到了约束解析器错误.

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

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

解决方案

该错误意味着您在 Route 中的某处指定了类似

[Route("SomeRoute/{someparameter:string}")]

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

如错误所示,Web API 附带的 DefaultInlineConstraintResolver 没有称为 string 的内联约束.默认支持的如下:

//特定于类型的约束{ "bool", typeof(BoolRouteConstraint) },{日期时间",类型(DateTimeRouteConstraint)},{十进制",typeof(DecimalRouteConstraint)},{ "double", typeof(DoubleRouteConstraint) },{浮动",类型(FloatRouteConstraint)},{ "guid", typeof(GuidRouteConstraint) },{ "int", typeof(IntRouteConstraint) },{ "long", typeof(LongRouteConstraint) },//长度约束{ "minlength", typeof(MinLengthRouteConstraint) },{ "maxlength", typeof(MaxLengthRouteConstraint) },{长度",typeof(LengthRouteConstraint)},//最小值/最大值约束{ "min", typeof(MinRouteConstraint) },{ "max", typeof(MaxRouteConstraint) },{范围",类型(RangeRouteConstraint)},//基于正则表达式的约束{ "alpha", typeof(AlphaRouteConstraint) },{正则表达式",类型(RegexRouteConstraint)}

I'm using Web API 2 and I'm getting the following error when I send a POST to my API method using IIS 7.5 on my local box.

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

Line 21: GlobalConfiguration.Configuration.EnsureInitialized();

None of my APIs work using IIS. However, I'm able to run my API project in Visual Studio using IIS Express and successfully make a POST to my login API, but when I try and make a GET request to another API call, I get the constraint resolver error.

In order to troubleshoot this, I created a brand new Web API 2 project in Visual Studio and started importing the existing APIs into the new project one at a time and running them to ensure they work. Using IIS Express with this new project, I get the same exact results as I did with my existing API project.

What am I missing here? Even with a brand new project I'm not able to make GET requests without running into this constraint resolver issue.

解决方案

The error means that somewhere in a Route, you specified something like

[Route("SomeRoute/{someparameter:string}")]

"string" is not needed as it is the assumed type if nothing else is specified.

As the error indicates, the DefaultInlineConstraintResolver that Web API ships with does not have an inline constraint called string. The default supported ones are the following:

// 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) }

这篇关于WebAPI 2 中的 DefaultInlineConstraintResolver 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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