F11
F11

Reputation: 3816

Get method conflicts not getting resolved in webapi

My webapi get methods inside MoviesController are

// GET api/movies
        /// <summary>
        /// Returns the sorted list of movies
        /// </summary>
        /// <returns>Collection of Movies</returns>
        public IEnumerable<Movie> Get()
        {
            return repository.GetMovies().OrderBy(c => c.Title);
        }

        /// <summary>
        /// Returns an individual movie
        /// </summary>
        public Movie Get(int movieId)
        {
            var movie = repository.GetMovieById(movieId);
            if (movie == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return movie;
        }

and WebApiConfig.cs is

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

The problem i am facing is each time Get() is getting called.Get with Id method is not getting called.I have tried various solutions given on SO,but it is not working for me.

Upvotes: 0

Views: 181

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

Make one more custom route as shown :

config.Routes.MapHttpRoute(
     name: "MyRoute",
     routeTemplate: "api/{controller}/{action}/{movieId}",
     defaults: new { movieId = "0" }  //instead of default movieid '0' you can specify as per your requirement.
   );

Upvotes: 1

Related Questions