WebApi中的内联约束错误

时间:2015-11-04 21:35:32

标签: c# asp.net-web-api asp.net-web-api2

我的webapi中的自定义路线。当我打电话给它时,我收到了以下错误

The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'MovieData'.

堆栈跟踪: at System.Web.Http.Routing.InlineRouteTemplateParser.GetInlineConstraint(Group constraintGroup, Boolean isOptional, IInlineConstraintResolver constraintResolver) at System.Web.Http.Routing.InlineRouteTemplateParser.ParseRouteTemplate(String routeTemplate, IDictionary 2个默认值,IDictionary 2 constraints, IInlineConstraintResolver constraintResolver) at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilder(String template, IInlineConstraintResolver constraintResolver) at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilderInternal(String template) at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilder(String template) at System.Web.Http.RouteAttribute.System.Web.Http.Routing.IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context) at System.Web.Http.Routing.DefaultDirectRouteProvider.CreateRouteEntry(String prefix, IDirectRouteFactory factory, IReadOnlyCollection 1个操作,IInlineConstraintResolver constraintResolver, Boolean targetIsAction) at System.Web.Http.Routing.DefaultDirectRouteProvider.CreateRouteEntries(String prefix, IReadOnlyCollection 1个工厂,IReadOnlyCollection 1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction)    在System.Web.Http.Routing.DefaultDirectRouteProvider.GetActionDirectRoutes(HttpActionDescriptor actionDescriptor,IReadOnlyList 1 factories, IInlineConstraintResolver constraintResolver)    在System.Web.Http.Routing.DefaultDirectRouteProvider.GetDirectRoutes(HttpControllerDescriptor controllerDescriptor,IReadOnlyList 1 actionDescriptors, IInlineConstraintResolver constraintResolver)    在System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector,HttpConfiguration配置,IInlineConstraintResolver constraintResolver,IDirectRouteProvider directRouteProvider)    在System.Web.Http.Routing.AttributeRoutingMapper。<> c__DisplayClass2。<> c__DisplayClass4.b__1() at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func 1初始化程序)    在System.Web.Http.Routing.AttributeRoutingMapper。<> c__DisplayClass2.b__0(HttpConfiguration config)    在System.Web.Http.HttpConfiguration.EnsureInitialized()    在System.Web.Http.GlobalConfiguration.Configure(Action 1 configurationCallback)    at MoviesAPI.WebApiApplication.Application_Start()in c:\ Users \ VinoBharathi \ Documents \ Visual Studio 2013 \ Projects \ MoviesAPI \ MoviesAPI \ Global.asax.cs:line 14`

ApiController:
[HttpGet]        [Route("~/api/Values/GetMovieByCategory/{movieData:MovieData}")]
        public IEnumerable<MovieData> GetMovieByCategory(MovieData movieData)
        {
IEnumerable<MovieData> movieDataByCat = null; string[] paramCast; if (movieData.Cast.Count() != 0)

                paramCast = movieData.Cast;
IEnumerable<MovieData> GetAllMovies = null;
GetAllMovies = repo.GetAll();

        if (movieData.Cast == null || movieData.Cast.Count() == 0)
        {
            movieDataByCat = from data in GetAllMovies
                             where (data.Classification == (movieData.Classification == null ? string.Empty : movieData.Classification) ||
                                    data.Genre == (movieData.Genre == null ? string.Empty : movieData.Genre) ||
                                    data.Rating == movieData.Rating ||
                                    data.ReleaseDate == movieData.ReleaseDate ||
                                    data.Title == (movieData.Title == null ? string.Empty : movieData.Title))

                             select data;
        }
        return movieDataByCat;
    }`

帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要实现名为MovieData的内联约束,并继承自IHttpRouteConstraint。 它在您的路线中声明:[路线(“〜/ api / Values / GetMovieByCategory / {movieData: MovieData }”)]

Ten在WebApiConfig文件中注册:

  var constraintResolver = new DefaultInlineConstraintResolver();
            constraintResolver.ConstraintMap.Add("MovieData", typeof(MovieDataRouteConstraint));
           config.MapHttpAttributeRoutes(constraintResolver);