ASP.NET WebApi doesn't work. All routes return 404
I have an app.net web api with Unity
both my dependent resolver and OWIN
for OAuth authentication.
I am Startup.cs
using Visual Studio to create an "Add New Item" menu by selecting the OWIN Startup Class :
[assembly: OwinStartup(typeof(MyNameSpace.Startup))]
namespace MyNameSpace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
config.DependencyResolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
app.UseWebApi(config);
}
}
}
Mine WebApiConfig.cs
looks like this:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Now when I run the app I get a Forbidden response for the default url which is http://localhost:port/
. The web api is hosted in http://localhost:port/api/
. When I make a request for this url or any controller in the application, it responds with Not Found .
Also, when I put a breakpoint in a Configuration
class method Startup
; as soon as I start the application, it displays the following (I don't know if this is valid):
I can't figure out what happened. He worked last night and I'm the only one who worked on this project. The only thing I did was add OData
links from NuGet
, but I removed them again as soon as I established that the api was not working.
EDIT:
I must add that any breakpoint set in the application is currently displaying the same message when I hover over it, so maybe this is relevant after all.
EDIT 2:
This is an excerpt from EmployeeController.cs
:
[Authorize]
public class EmployeeController : ApiController
{
private readonly IEmployeeService _service;
public EmployeeController(IEmployeeService employeeService)
{
_service = employeeService;
}
[HttpGet]
[ResponseType(typeof(Employee))]
public IHttpActionResult GetEmployee(string employeeId)
{
var result = _service.GetEmployees().FirstOrDefault(x => x.Id.Equals(employeeId));
if (result != null)
{
return Ok(result);
}
return NotFound();
}
[HttpGet]
[ResponseType(typeof (IQueryable<Employee>))]
public IHttpActionResult GetEmployees()
{
return Ok(_service.GetEmployees());
}
...
EDIT 3
After restarting Visual Studio as suggested, I can confirm that the breakpoint warning persists and appears throughout the application:
EDIT 4
Removing OWIN
links and Startup.cs
, the app is now back to life. I can place breakpoints again and make api calls. What's happening?
WebAPI action methods are based on the HTTP verb.
If you want to name an action method other than HTTP-Verb, you want to look at Attribute Routing .
In your example, you are using a simple HTTP verb. If so, you just need to Receive .
public class EmployeeController : ApiController
{
// GET api/employee
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/employee/5
public string Get(int id)
{
return "value";
}
}
Just a point about the breakpoints not being linked, I had this problem. Realized that after I published the app to the webserver, VS2013 actually updated my web.config to match my web.config transform I had to post and disabled debugging (as per my transform). Maybe I'm a beginner and this is the correct behavior, but it wasn't the desired behavior! :) This post is old I know, but if it helps anyone with the same symptoms, great.