Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't navigate to web api controller

I use Web API for the first time to connect my mobile app to Web API.
I have MVC 5 project and I have created API folder and inside I have created ProductsController.

namespace DDD.WebUI.API
{
    public class ProductsController : ApiController
    {
        public string GetAllProducts()
        {
            return "Hello From WebAPI";
        }

    }
}

And I have tried to access this method from browser:

http://localhost:21879/DDD.WebUI/API/products/GetAllProducts

But I get 404.
It's probably something stupid but can't figure it :(
UPDATE

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               name: "DefaultApi",
               url: "API/{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );



        }

UPDATE 2
I have a little progress: I have update Application_Start() with:

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);

In WebApiConfig I have:

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

I still store API controller in Project/Api/ProductsController But now I can access it but still can't get value from it:

http://localhost:21879/api/products/getallproducts

But error I get is:

<Error>
<Message>The request is invalid.</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'GSL.WebUI.Api.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
</MessageDetail>
</Error>
like image 222
1110 Avatar asked Aug 25 '14 10:08

1110


3 Answers

I had the same problem. Watch the order in which you register your routes in Global.asax.cs.

This works:

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);

This doesn't:

RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
like image 132
Kevin Avatar answered Oct 04 '22 03:10

Kevin


If you use that way of routing, you need to specify the allowed Http methods for the action:

public class ProductsController : ApiController
{
    [HttpGet]
    public string GetAllProducts()
    {
        return "Hello From WebAPI";
    }
}

You can get all the information regarding API routing from this link: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

like image 35
Tallmaris Avatar answered Oct 04 '22 03:10

Tallmaris


I think looking at your update your action requires id which is of non-nullable int and you are not supplying it just modify url as http://localhost:21879/API/products/GetAllProducts/2 here '2' is assigned to 'id' and it will work.

In WebApiConfig modify route as :

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

Just change this Mvc Route :

    routes. MapRoute(
           name: "DefaultApi" ,
           url: "API/{controller}/{action}/{id}" ,
           defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
       );

with this :

     routes. MapRoute(
           name: "DefaultApiMvc" ,
           url: "APIMvc/{controller}/{action}/{id}" ,
           defaults: new { controller = "Home" , action = "Index" , id = UrlParameter .Option
       );

because otherwise Mvc and Api routes will conflict with each other...

like image 35
Kartikeya Khosla Avatar answered Oct 04 '22 03:10

Kartikeya Khosla