调用WebApi出现 远程服务器返回错误: (500) 内部服务器错误

一、检查错误错误

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

二、解决问题

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

则修改WebApi中的路由

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

posted @ 2019-07-12 16:19  haishu  阅读(4165)  评论(0编辑  收藏  举报