gpt4 book ai didi

c# - Web Api 发现多个操作

转载 作者:太空狗 更新时间:2023-10-29 23:21:40 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com