简体   繁体   中英

ASP.NET WebApi not working. All routes return 404

I have an asp.net web api with Unity as my dependency resolver and OWIN for OAuth authentication.

I create a Startup.cs using the Visual Studio "Add New Item"-menu, choosing 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);
        }
    }
}

My 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 start the application I get a Forbidden response for the default url which is http://localhost:port/ . The web api is hosted at http://localhost:port/api/ . When I make a request for this url or any controller in the application it responds with Not Found .

Additionally, when I put a breakpoint in the Configuration method of the Startup class; once I start up the application it displays the following (I don't know if it is relevant):

信息

I can't figure out what's wrong. It worked last night and I'm the only one who has been working on this project. The only thing I've done is add OData references from NuGet , but I removed those again once I established that the api was not working.

EDIT:

I should add that any breakpoint I set in the application currently displays the same message when I hover over it, so maybe it 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 shows up accross the entire application:

控制器中的断点

EDIT 4

Removing OWIN references and Startup.cs , the application now springs back into life. I am able to place breakpoints again and make api calls. What's going on?

WebAPI Action Methods are based on HTTP Verb.

If you want to name action method other than HTTP Verb, you want to look at Attribute Routing .

In your example, you are using simple HTTP Verb. If so, you just need Get .

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 engaging, I had this problem myself. Realised that after I published the app to the web server, VS2013 had actually updated my web.config in accordance with my web.config transform I had for publish, and had turned off the debugging (as per my transform). It may be me being a newb and this is the correct behaviour, but it wasn't the desired behaviour! :) This post is old, I know, but if it helps anyone with the same symptoms, excellent.

I had the same problem, which all endpoints returned 404. I notice that the WebApiConfig.Register never called. So make sure it is called, and if you have a Global.asax file on your project (like in my case) make sure you have this call enabled on Application_Start:

    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);          
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM