F11
F11

Reputation: 3816

Error in running web api

I have a asp.net web api project in which there is only one api controller and inside that one get method,nothing else.when i try to run it,it is showing below error

HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.




    [CacheOutput(ClientTimeSpan = 300, ServerTimeSpan = 300)]
    public IEnumerable<Movie> Get()
    {
        return repository.GetEmployees().OrderBy(c => c.EmpId);
    }

and the url is http://domain:53538/api/employees

and WebApiConfig is

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

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
             name: "SearchApi",
             routeTemplate: "api/{controller}/{action}/{searchstr}"
         );

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

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

        }

Upvotes: 0

Views: 101

Answers (2)

Arindam Nayak
Arindam Nayak

Reputation: 7462

In your code , i can see that following corresponds to same routing url.

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

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

NOTE: In both cases last param is marked as optional, so route is left as api/{controller} and when you used api/movies, routing system got confused and stopped working!

So you just need to re-arrange routing url, i would suggest replace 1st one ( in my answer to this)

config.Routes.MapHttpRoute(
               name: "DefaultApiWithMovieId",
               routeTemplate: "api/{controller}/{movieId}"

           );

So this will make the movieid param as mandatory, it will have url /api/movies/2 , here 2 is movie id

and the later will correspond to api/movies( as id here is optional - referring to 2nd code block in my answer) , so that your solution will work,

Upvotes: 0

Veritoanimus
Veritoanimus

Reputation: 319

the most likely reason is that you're missing this or something similar in your Global.asax Application_Start

GlobalConfiguration.Configure(config =>
{

    config.MapHttpAttributeRoutes();

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

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));


});

This is what tells the application how to map to your Web API Controller. That said, you may run into a different problem afterwards depending on the structure of your entity. I've found that Entities with relationships don't serialize well and can often throw a serialization error because it creates a circular reference, so you may need to go into a custom object for the purpose of serialization. Without a lot more information it's difficult to say for certain what the problem is. There could also be with the API call or with the structure of the Controller.

The model I use would have you calling

$.get('/api/movies/get',,function(result) {});

which is why I generally specify the httpmethod and give the api call a meaningful name.

Hopefully this gives enough to figure it out from there.

EDIT AFTER SEEING UPDATE

It looks like you have too much in your mapping. Simplify your mapping and just use your WebAPI on 1 map. Otherwise it just confuses the routing and doesn't know which map to go to.

In your mapping you have 2 different controller maps that could handle the same request. (the second and third) They both read the pattern 'api/[^/]+(/[^/]*)?'

Upvotes: 1

Related Questions