WebAPI No action was found on the controller

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

I got an error - No action was found on the controller 'Action' that matches the request.

The url is http://localhost:37331/api/action/FindByModule/1.

The routing I used is

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

Controller:

public class ActionController : ApiController {     private IActionRepository repository = null;      [HttpGet]     [ActionName("All")]     public IEnumerable<JsonAction> All()     {         return from action in this.repository.Get()                select new JsonAction                {                    ID = action.ID,                    Text = action.Text.Trim(),                    Description = action.Description.Trim(),                };     }      [HttpGet]     [ActionName("FindByModule")]     public IEnumerable<JsonAction> FindByModule(Int64 moduleId)     {         return from action in this.repository.FindByModule(moduleId)                select new JsonAction                {                    ID = action.ID,                    Text = action.Text.Trim(),                    Description = action.Description.Trim(),                };     } } 

回答1:

This is because there is a parameter name mismatch. From your route the value 1 is assigned to parameter named id and your action is looking for parameter named moduleId.

First option is to change your route like this:

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

Second is to change your URL like this:

http://localhost:37331/api/action/FindByModule?moduleId=1 

So the parameter name match.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!