C# WebApi 控制器没有找到控制器的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12229297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:31:36  来源:igfitidea点击:

WebApi Controller no action was found for the controller

c#asp.netasp.net-mvc-3

提问by dooburt

A real head-scrather this one. I have created two ApiControllers which I am using as a JSON webservice:-

一个真正的头疼这个。我创建了两个 ApiControllers,我将它们用作 JSON 网络服务:-

namespace ControlTower.Controllers
{
    public class AirlinesController : ApiController
    {
        private static IEnumerable<Airline> MapAirlines()
        {
            return (Jetstream.AirlineObject.GetAirlines()).Select(x => x);
        }

        [HttpGet]
        public IEnumerable<Airline> GetAirlines()
        {
            return MapAirlines().AsEnumerable();
        }

        [HttpGet]
        public Airline GetAirlineByCode(string code)
        {
            return Jetstream.AirlineObject.GetAirline(code);
        }
    }
}

and:-

和:-

namespace ControlTower.Controllers
{
    public class ReviewsController : ApiController
    {
        private static IEnumerable<Review> MapReviews(int airline)
        {
            return (Jetstream.ReviewObject.GetReviews(airline)).Select(x => x);
        }

        [HttpGet]
        public IEnumerable<Review> GetReviews(int airline)
        {
            return MapReviews(airline).AsEnumerable();
        }

        [HttpGet]
        public Review GetReviewById(int review)
        {
            return Jetstream.ReviewObject.GetReview(review);
        }
    }
}

With this routing:-

使用此路由:-

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

And whilst visiting /api/airline/get/baor /api/airline/get/works perfectly, visiting any variation of Reviewdoes not. Can anyone see anything really obvious I'm missing here?

在访问/api/airline/get/ba/api/airline/get/完美运行时,访问Review 的任何变体都没有。任何人都可以看到我在这里遗漏的任何非常明显的东西吗?

Help is appreciated.

帮助表示赞赏。

采纳答案by Jason Bouchard

Your default route is expecting a parameter named "code". You either need to add a route to accept a parameter named airline and/or review, or explicitly tell the controller the name of the parameter.

您的默认路由需要一个名为“code”的参数。您需要添加一条路线以接受名为航空公司和/或评论的参数,或者明确告诉控制器该参数的名称。

ex. /api/reviews/get?airline=1

前任。/api/reviews/get?airline=1