C# 防止Web API映射意外路由

C# 防止Web API映射意外路由,c#,asp.net,routes,asp.net-web-api2,C#,Asp.net,Routes,Asp.net Web Api2,我在WebAPI2中使用路由映射,遇到了以下问题 我配置了以下两条路由: new { Path = "api/widgets/{group}/{name}", Defaults = new { }, Controllers = new[] { new { Name = "Widget", Type = typeof(WidgetController) } } }, new { Path = "api/widgets/{group}"

我在WebAPI2中使用路由映射,遇到了以下问题

我配置了以下两条路由:

new
{
    Path = "api/widgets/{group}/{name}",
    Defaults = new { },
    Controllers = new[]
    {
        new { Name = "Widget", Type = typeof(WidgetController) }
    }
},
new
{
    Path = "api/widgets/{group}",
    Defaults = new { group = RouteParameter.Optional },
    Controllers = new[]
    {
        new { Name = "WidgetGroup", Type = typeof(WidgetController) }
    }
}
WidgetController定义了以下方法:

// 1) Create a new widget group
public IHttpActionResult PostAsync(WidgetGroup entity) { ... }

// 2) Create a new widget in a widget group
public IHttpActionResult PostAsync(string group, Widget entity) { ... }

// 3) Modify a specific widget in the widget group
public IHttpActionResult PutAsync(string group, string name, Widget entity) { ... }
发生的情况如下:

POSTapi/widgets映射到#1

PUTapi/widgets返回405方法不允许

POSTapi/widgets/mygroup映射到#2

PUTapi/widgets/mygroup返回405方法不允许

PUTapi/widgets/mygroup/mywidget映射到#3

所有这些都是预期的,并按预期工作。然而:

POSTapi/widgets/mygroup/mywidget映射到#2

我不希望这种情况发生

如何防止这种情况并使其返回405 method not allowed