我使用的是Web API 2,当我在本地邮箱上使用IIS 7.5向我的API方法发送帖子时,出现了以下错误.

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

Line 21: GlobalConfiguration.Configuration.EnsureInitialized();

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

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

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

推荐答案

该错误表示您在路径中的某个位置指定了如下内容

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

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

如错误所示,Web API附带的DefaultInlineConstraintResolver没有名为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) }

Asp.net相关问答推荐

使用 aspnet core SignalR 客户端为 VSIX 项目.

威胁已经被清除了

如何判断 IIS 是处于 32 位还是 64 位模式

使用 JavaScript 禁用 ASP.NET 验证器

MvcBuildViews true 与 ASP.NET MVC 2 中的实体框架

在 Asp.net mvc5 中使用用户名而不是邮箱作为身份

Web Api 参数始终为空

如何从 JS 访问 ViewBag

在asp.net mvc 3中实现FilterAttribute,IActionFilter和从ActionFilterAttribute继承有什么区别?

修改 web.config 时如何防止 ASP.NET 应用程序重新启动?

如何将表从存储过程检索到数据表?

VS2017 无法加载文件或程序集 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 或其依赖项之一

Cache.Add 绝对过期 - 是否基于 UTC?

在 ASP.NET 中使用 MasterPages 时使用 JQuery 的正确方法?

当文件上传超出 ASP.NET MVC 中允许的大小时显示自定义错误页面

将 C# ASP.NET 数组传递给 Javascript 数组

GZIP 与 DEFLATE 压缩相比有什么优势?

DropDownList 的 SelectedValue 与 SelectedItem.Value

在控制器 asp.net-core 中获取当前区域性

Session.Clear() 与 Session.RemoveAll()