bar*_*oma 5 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 };
}
}
Run Code Online (Sandbox Code Playgroud)
我的请求类型是这样的:
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; }
}
Run Code Online (Sandbox Code Playgroud)
路线在这里:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
当我http://localhost:52285/api/My/UpdateFeature通过fiddler 发送request()时,它返回HTTP/1.1 500内部服务器错误
错误信息是:
{"message":"发生了错误.","exceptionMessage":"找到了与请求匹配的多个操作:类型为WebGUI.Controllers.MyController的类型为WebGUI.Controllers.MyController\r \nDeleteFeature的\ r \nUpdateFeature" ,"exceptionType":"System.InvalidOperationException","stackTrace":".....
您的路线是错误的,因为它没有指定操作名称,因此它将该UpdateFeature部分视为 ID 参数。改成这样:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1104 次 |
| 最近记录: |