调用WebApi(500)内部服务器错误

码农公社  210.net.cn   210= 1024  10月24日一个重要的节日--码农(程序员)节

一、检查错误

将 

HttpWebResponse response = (HttpWebResponse)request.GetResponse();


改为 

HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}


二、解决问题

如果抛出异常为:"找到了与该请求匹配的多个操作"


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


则修改上面的WebApi路由设置为如下:


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


评论