ata*_*xia 2 asp.net asp.net-core asp.net-core-3.0
我有一个非常令人困惑的问题,主要是因为我得到的异常非常无益且非描述性。
我有一个基于 ASP.Net Core 的 API,并且刚刚添加了一个新控制器。我正在使用 ASP.Net Core 3.0,并使用以下命令在 Startup.Configure 中映射我的控制器:
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
但是,在调试中运行 API 时,我在启动时遇到以下异常:
RoutePatternException:路由模板中存在不完整的参数。检查每个“{”字符是否都有匹配的“}”字符。
我删除了控制器,问题就消失了,所以我认为控制器有问题,导致端点映射抛出此异常,但我看不到它是什么?
只有当我重新配置端点的路由方式时,我才找到问题的答案。我将其写下来,以防其他人从 ASP.Net Core 获得任何非描述性异常。在 Startup.Configure 中,我替换了:
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
和:
app.UseMvc(routes =>
{
routes.MapRoute("repo1", "{controller=Repo1Controller}");
routes.MapRoute("repo2", "{controller=Repo2Controller}");
}
Run Code Online (Sandbox Code Playgroud)
然后在 Startup.ConfigureServices 中,我添加了:
services.AddMvc(option => option.EnableEndpointRouting = false);
下次启动时,显示以下异常:
RouteCreationException:属性路由信息发生以下错误:对于操作:“MyAPI.Controllers.Repo2Controller.UpdateEntityProperty(MyAPI)”
错误:路由模板中存在不完整的参数。检查每个“{”字符是否都有匹配的“}”字符。(参数“路由模板”)
在Repo2Controller我有以下内容:
[HttpPut("{entityId}/Properties/{propertyId")]
public IActionResult UpdateEntityProperty(string entityId, string propertyId)
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud)
这突出表明我错过了属性}中的结束语HttpPut。一旦我替换了它并返回到原始端点路由方法,一切都工作正常。
| 归档时间: |
|
| 查看次数: |
5049 次 |
| 最近记录: |