Can't get Web API 2 to work

Vote:
 

Trying to get Web API working but I'm just getting 404s no matter what I do.

// Global.asax

GlobalConfiguration.Configure(WebApiConfig.Register);

// WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
  config.MapHttpAttributeRoutes();
}

// ApiController in directory /ApiControllers

[RoutePrefix("api/requestpassword")]
public class RequestPasswordController : ApiController
{
  [Route("")]
  public HttpResponseMessage Get()
  {
    return Request.CreateResponse(HttpStatusCode.OK);
  }
}

// Request is localhost:37001/api/requestpassword GET

 

Any ideas? I'm all out...

#78941
Dec 05, 2013 11:44
Vote:
 

Looks like the route is empty?

e.g.: [Route("customers/{customerId}/orders")]

See: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

#78958
Dec 05, 2013 15:46
Vote:
 

Having similar issue. Any Solution for this?

#111685
Oct 17, 2014 13:55
Vote:
 

And a little bump. Had it working, then after a conflict it stopped working. Did you find out what the problem was?

#119918
Apr 07, 2015 15:01
Vote:
 

A restart of VS and suddenly it worked again....

#119919
Apr 07, 2015 15:18
Vote:
 

Which version of .Net are you using?

#120019
Apr 08, 2015 22:02
Vote:
 

I don't know if this is the issue but if you are using System.Web.Mvc.RoutePrefix and System.Web.Mvc.Route instead of System.Web.Http.RoutePrefix System.Web.Http.Route the routes will not be registered for your ApiControllers. So, check your attributes and then it should work just fine.

/Marcus

#120080
Apr 09, 2015 23:18
Vote:
 

Like Marcus is saying, it is important to add routes the right way, that is descibed in the linked document

This adds a normal default route

routes.MapRoute(name: "api", url: "api/{controller}/{action}", defaults: new { controller = "Login", action = "index" });

and this adds a default route for webapi

routes.MapHttpRoute(name: "API Default",routeTemplate: "webapi/{controller}/{id}",defaults: new { id = RouteParameter.Optional });
#120086
Apr 10, 2015 7:40
Vote:
 

ok, my 2 cents ;) We discovered in our projects, that 4.5.1 and episerver and webapi somehow didn't play well together.. routing was not working

#120089
Apr 10, 2015 7:51
Vote:
 

Interesting Valdis, we are using WebApi2 in a 8.1 project with MVC5 and it seems to work fine

#120091
Apr 10, 2015 8:17
Vote:
 

We're using .NET 4.5.1. I switched to IIS Express and it started working, then switched back to regular IIS and it worked. A bit spooky, but I'm happy... :)

#120094
Apr 10, 2015 8:52
Vote:
 

Erik, originally - when you where on IIS with 4.5.1 - it didn't work? You switched to express, then back - and it started to work?

#120098
Apr 10, 2015 9:43
Vote:
 

Valdis, it worked first. But "suddenly" it stopped working (TFS conflicts, EPiServer updates etc). When switching to IIS Express and started to work, and switching back it worked again. Still havent figured out what the problem was, and I will not try to unless it happens again. :) Haven't changed .NET Framework during the process.

#120099
Apr 10, 2015 9:49
Vote:
 

I've had a similar problem - I wanted to extend the new ServiceAPI with my own additional services - and added them using attribute routing - and no matter what I did, it simply wouldn't work...

Then, after updating my project to .NET 4.5.2 it started working all of a sudden (before it was 4.5.1). If it's the updated .NET or just the included reboot and restart that made it work I don't know.

#120344
Apr 15, 2015 14:11
Vote:
 

Hi Henrik

In your second route registration, do you intentionally exclude the "action" from the template? Is there any specific reason?

Thanks,

Vincent

#144620
Feb 16, 2016 11:35
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.