ASP.Net Core 3.0 应用启动异常'There is an incomplete parameter in the route template'

ASP.Net Core 3.0 app startup exception 'There is an incomplete parameter in the route template'

我有一个非常令人困惑的问题,主要是因为我得到的异常非常无用且没有描述性。

我有一个 ASP.Net 基于内核的 API,并且刚刚添加了一个新控制器。我正在使用 ASP.Net Core 3.0,并使用以下方法在 Startup.Configure 内映射我的控制器:

app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

但是,当 运行 调试 API 时,我在启动时遇到以下异常:

RoutePatternException: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character.

我删除了控制器并且问题消失了,所以我认为控制器有问题导致端点映射抛出此异常但我看不到它是什么?

只有当我重新配置端点的路由方式时,我才找到问题的答案。我已经把它写出来以防其他人从 ASP.Net Core 获得任何非描述性异常。在 Startup.Configure 内,我替换了:

app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

与:

app.UseMvc(routes =>
{
    routes.MapRoute("repo1", "{controller=Repo1Controller}");
    routes.MapRoute("repo2", "{controller=Repo2Controller}");
}

然后在Startup.Configure服务中,我添加:

services.AddMvc(option => option.EnableEndpointRouting = false);

下次启动时会显示以下异常:

RouteCreationException: The following errors occurred with attribute routing information: For action: 'MyAPI.Controllers.Repo2Controller.UpdateEntityProperty (MyAPI)'

Error: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character. (Parameter 'routeTemplate')

Repo2Controller 我有以下内容:

[HttpPut("{entityId}/Properties/{propertyId")]
public IActionResult UpdateEntityProperty(string entityId, string propertyId)
{
    // Do some stuff
}

这突出了我在 HttpPut 属性中缺少结束符 } 的事实。一旦我替换它并返回到原始端点路由方法,一切正常。

try catch 包装 Configure() 方法代码,如下所示。

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        try
        {
            ...

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            ...

        }
        catch (System.Exception exp)
        {

            throw;
        }
    }

然后调查抛出的异常。在 InnerException->Pattern 下,您会找到特定的问题。示例: