最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

十年河东十年河西,莫欺少年穷。 

   本人最近在研究C#webAPI相关知识,发现webAPI不能够支持多个Get方法,这些Get方法有如下一特点:

相同数量的参数,这些参数类型可以不相同。奇怪的是:即使这些方法的返回值不同,方法名不同,但在程序请求执行过程中会出现如下错误提示:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Multiple actions were found that match the request: System.Net.Http.HttpResponseMessage GetById(Int32) on type WebApiTest.Controllers.PersonController System.String GetBySex(System.String) on type WebApiTest.Controllers.PersonController
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
</StackTrace>
</Error>

譬如:如下两个方法:

[HttpGet]
public HttpResponseMessage GetById(int Id)
{
list = list.Where(p => p.Id == Id).ToList();
return ResultToJson.toJson(list);
} [HttpGet]
public HttpResponseMessage GetByName([FromUri]string Name)
{
list = list.Where(p => p.Name == Name).ToList();
return ResultToJson.toJson(list);
}

在请求过程中就会报上述错误,究其原因,是因为我们在Get请求时,两个方法都需要接收一个参数,导致了:不知道应该执行哪个方法的问题。

你可能会问:我写的方法名不一样,并且在Get请求时,明确了请求的是哪个方法,为什么还会报错?

究其原因,是因为WebApiConfig的配置引起的,在你新建的项目中,webApiConfig的配置是不指向Action的,初始的webApiConfig如下:

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

routeTemplate: "api/{controller}/{id}",从这句可以看出,和Action没有任何毛关系,所以,GEt请求时:即使你指定了方法名,也会报错。

因此:我们有必要修改下这个配置,修改成指向特定的Action,也就解决了上述问题。修改后的代码如下:

 public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional }
);
}

所以嘛,我认为VS项目初始化脑残,故意给我们程序员找麻烦,明知道潜在的问题,TMD就是不修复,还得我们自己百度找答案!

@陈卧龙的博客

解决webApi<Message>An error has occurred.</Message>不能写多个Get方法的问题的更多相关文章

  1. iis 7 -mvc WebApi {"message":"an error has occurred"}

    iis 7 - WebApi's {"message":"an error has occurred"} 原因是web api的Controller有两个类名重 ...

  2. Swagger发布服务器时错误 500 : { "Message": "An error has occurred." }

    在做Web API的文档自动生成时,本机调试都正常,发布到服务器上出现500错误 500 : { "Message": "An error has occurred.&q ...

  3. 解决MyEclipe出现An error has occurred,See error log for more details的错误

    今晚在卸载MyEclipse时出现An error has occurred,See error log for more details的错误,打开相应路径下的文件查看得如下: !SESSION 2 ...

  4. idhttp post 出现exception class EIdSocketError with message 'Socket Error # 10054的解决办法(捕捉异常,防止程序挂掉)

    由于项目需要.需要向一个地址提交数据.提交的次数很频繁,但是程序经常raised exception class EIdSocketError with message 'Socket Error # ...

  5. nova instance出错:"message": "Proxy error: 502 Read from server failed

    执行 $ nova resize instance1 时候出错: {, "details": " File \"/opt/stack/nova/nova/com ...

  6. eclipse启动出现“An Error has Occurred. See the log file”解决方法

    最近在启动eclipse时出现了“An Error has Occurred. See the log file”的错误,点击确定后也不能启动eclipse.查看log文件,出现类似: java.la ...

  7. [PHP][REDIS]phpredis 'RedisException' with message 'read error on connection'

    最近一个后台常驻job通过redis的brpop阻塞读取消息时,设置了永不超时 while( $re=$redis->brPop($queue_name,0) ){ } 但是在实际的使用中发现很 ...

  8. AndroidStudio3.0无法打开Android Device Monitor的解决办法(An error has occurred on Android Device Monitor)

    ---恢复内容开始--- 打开monitor时出现 An error has occurred. See the log file... ------------------------------- ...

  9. ERROR: Cannot load message class for [speech_control/command]. Are your messages built?

    ubuntu14.04 ROS indigo 问题: 执行查看指定消息的命令,出现下面的错误提示,找不到该消息类型. ~$ rostopic echo /speech/command ERROR: C ...

随机推荐

  1. NV Maxwell architecture

    按照NVIDIA的路线图来看,GTX 600以及GTX 700系列所采用的Kepler架构已经垂垂老矣,最早在明年第一季度,其继任者Maxwell架构可能就会和我们正式见面了.目前外媒已经放出了关于M ...

  2. RDBMS vs. NoSQL 合作还是竞争

    欢迎转载,转载请注明出处,徽沪一郎. 由于近期手头的工作和数据库的选型相关,纠结于是否使用一款NoSQL数据库来替换已有的MySQL数据库.在这个过程中随着学习研究的深入,对于二者的异同有了一些初步的 ...

  3. PHP 设计模式 笔记与总结(6)基础设计模式:工厂模式、单例模式和注册树模式

    三种基础设计模式(所有面向对象设计模式中最常见的三种): ① 工厂模式:使用工厂方法或者类生成对象,而不是在代码中直接new 在 Common 目录下新建 Factory.php: <?php ...

  4. 代理和block反向传值

    代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...

  5. 给Android程序员的六个建议

    给Android程序员的六个建议 分类: 安卓相关2015-07-14 23:58 177人阅读 评论(0) 收藏 举报 android程序员 如果你一年前写的代码 , 在现在看来你还感觉写的很不错 ...

  6. wordpress页面前端添加编辑按钮

    <?php edit_post_link(__('Edit This')); ?> 在single.php或者page.php模板页面加入以上代码片段.当管理员登录后,可以直接点击编辑文章 ...

  7. memcached学习笔记5--socke操作memcached 缓存系统

    使用条件:当我们没有权限或者不能使用服务器的时候,我们需要用socket操作memcached memcached-client操作 特点: 无需开启memcache扩展 使用fsocketopen( ...

  8. MVC设计模式

    随着Web应用的商业逻辑包含逐渐复杂的公式分析计算.决策支持等,使客户机越 来越不堪重负,因此将系统的商业分离出来.单独形成一部分,这样三层结构产生了. 其中‘层’是逻辑上的划分. 三层体系结构是将整 ...

  9. WGZX:javaScript 学习心得--1

    标签: javascriptiframedreamweaver浏览器htmltable 2008-09-11 10:50 1071人阅读 评论(0) 收藏 举报  分类: UI(21)  1,docu ...

  10. 使用ASP.NET web API创建REST服务(二)

    Creating a REST service using ASP.NET Web API A service that is created based upon the architecture ...