在 Swagger 中输入 OData 字符串参数时未使用单引号“立即尝试”时出现 Microsoft.OData.ODataException
我将一个 Web api 项目从 asp.net v4 迁移到 asp.net core v6。
微软 AspNetCore OData:v8.08
Swashbuckle AspNetCore:v6.2.3
在新环境中,当我在 Swagger 中输入 OData 路由中包含的字符串参数时,我必须用单引号括起来尝试一下。
如果我省略单引号,则请求 URL 中的参数不会包含单引号,并且会出现异常:
https://localhost:7171/odata/test(id123)
Microsoft.OData.ODataException: The请求中的键值(测试)无效。键值应为“Edm.String”类型的格式。 ---> Microsoft.OData.ODataException:需要文字类型标记,但找到标记“test”。 在 Microsoft.OData.UriParser.ExpressionLexerLiteralExtensions.ReadLiteralToken(ExpressionLexer expressionLexer) 在 Microsoft.OData.ODataUriUtils.ConvertFromUriLiteral(字符串值、ODataVersion 版本、IEdmModel 模型、IEdmTypeReference typeReference) 在 Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext 上下文) --- 内部异常堆栈跟踪结束 --- 在 Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext 上下文) 在 Microsoft.AspNetCore.OData.Routing.Template.DefaultODataTemplateTranslator.Translate(ODataPathTemplate 路径,ODataTemplateTranslateContext 上下文) 在 Microsoft.AspNetCore.OData.Routing.ODataRoutingMatcherPolicy.ApplyAsync(HttpContext httpContext,CandidateSet 候选人) 在 Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext,IEndpointSelectorPolicy[] 策略,CandidateSet CandidateSet) 在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatch|8_1(EndpointRoutingMiddleware 中间件、HttpContext httpContext、任务 matchTask) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
在 asp.net v4 Swagger 页面中,我可以输入字符串参数,而无需用单引号引起来。 如果我在 .net 6 项目中将操作更改为非 OData 路由,我还可以在 Swagger 中输入字符串参数,而无需使用单引号尝试一下。
我该怎么做才能使该功能也适用于我的 .net v6 项目中的 OData 路由?
代码摘录:
[Route("odata")]
public class testController : ODataController
{
[HttpGet("test({id})")]
public IActionResult Get(string id)
{
}
}
I migrated a web api project from asp.net v4 to asp.net core v6.
Microsoft AspNetCore OData: v8.08
Swashbuckle AspNetCore: v6.2.3
In the new environment I have to enclose the string parameters that are included in the OData route with single quotes, when I enter it in Swagger try it out.
If I omit the single quotes, the parameter in the request URL doesn't have enclosing single quotes and I get an exception:
https://localhost:7171/odata/test(id123)
Microsoft.OData.ODataException: The key value (test) from request is not valid. The key value should be format of type 'Edm.String'.
---> Microsoft.OData.ODataException: Expected literal type token but found token 'test'.
at Microsoft.OData.UriParser.ExpressionLexerLiteralExtensions.ReadLiteralToken(ExpressionLexer expressionLexer)
at Microsoft.OData.ODataUriUtils.ConvertFromUriLiteral(String value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
at Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext context)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.OData.Routing.Template.KeySegmentTemplate.TryTranslate(ODataTemplateTranslateContext context)
at Microsoft.AspNetCore.OData.Routing.Template.DefaultODataTemplateTranslator.Translate(ODataPathTemplate path, ODataTemplateTranslateContext context)
at Microsoft.AspNetCore.OData.Routing.ODataRoutingMatcherPolicy.ApplyAsync(HttpContext httpContext, CandidateSet candidates)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
In the asp.net v4 Swagger page I can enter the string parameters without enclosing single quotes.
And if I change my operation to Non-OData routing in my .net 6 project, I also can enter the string parameters without enclosing single quotes in Swagger try it out.
What can I do to make this work also with OData routing in my .net v6 project?
Code extract:
[Route("odata")]
public class testController : ODataController
{
[HttpGet("test({id})")]
public IActionResult Get(string id)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论