c# - Web Api 发现多个操作

标签 c# asp.net-mvc asp.net-web-api

我有具有不同类型参数的操作。

public class MyController : ApiController
{       
    [HttpPost]
    public UpdateFeatureResponse UpdateFeature(UpdateFeatureResuest reqResuest)
    {
        return new UpdateFeatureResponse { IsSuccess = true };
    }

    [HttpPost]
    public DeleteFeatureResponse DeleteFeature(DeleteFeatureRequest request)
    {
        return new DeleteFeatureResponse{ IsSuccess = true };
    }

}

我的请求类型是这样的:

public class UpdateFeatureResuest
{
    public int Id { get; set; }
    public string Feature { get; set; }
}

public class UpdateFeatureResponse
{
    public bool IsSuccess { get; set; }
}

public class DeleteFeatureRequest
{
    public int Id { get; set; }
}

public class DeleteFeatureResponse
{
    public bool IsSuccess { get; set; }
}

路线在这里:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

当我通过 fiddler 发送请求 (http://localhost:52285/api/My/UpdateFeature) 时,它返回 HTTP/1.1 500 Internal Server Error

错误信息是:

{"message":"An error has occurred.","exceptionMessage":"Multiple actions were found that match the request: \r\nUpdateFeature on type WebGUI.Controllers.MyController\r\nDeleteFeature on type WebGUI.Controllers.MyController","exceptionType":"System.InvalidOperationException","stackTrace":" .....

最佳答案

您的路线是错误的,因为它没有指定操作名称,因此它将 UpdateFeature 部分视为 ID 参数。将其更改为:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

关于c# - Web Api 发现多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741733/

相关文章:

c# - 使用 Ninject 内核作为工作单元对象工厂

C# 设置信封的打印区域

c# - Web Api 2 不反序列化 JSON

c# - WebApi 路由返回 Not Found in Orchard Module

c# - Web API 动态 LINQ 搜索

javascript - 如何防止ajax中给出的 header 值的可见性以进行身份​​验证

c# - 如何配置 StructureMap 以使用通用存储库?

c# - 为非托管程序集动态创建包装器?

jquery - 支持使用 jquery 对 html 表行重新排序并使 SQL 后端快速运行的最佳方法?

c# - .NET (Visual Studio) 在项目之间共享 Assets