4

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):

message

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:

breakpoint in controller

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?

transporter_room_3
  • 2,583
  • 4
  • 33
  • 51
  • Can you show one of your API actions? – Babak Naffas May 05 '15 at 18:07
  • I edited my question to include code from a controller. – transporter_room_3 May 05 '15 at 18:17
  • That breakpoint issue usually gets resolved with Visual Studio being restarted. – Drew Kennedy May 05 '15 at 18:20
  • I edited my question to add another screenshot of the breakpoint warning occurring even after a restart. – transporter_room_3 May 05 '15 at 18:27
  • Take a look at this: http://stackoverflow.com/questions/2155930/fixing-the-breakpoint-will-not-currently-be-hit-no-symbols-have-been-loaded-fo – Drew Kennedy May 05 '15 at 18:29
  • I feel like I'm getting closer to the problem now. Using the resource you provided I can now see that no user code modules are loaded. And when I select "Break All" I get the following: "Source Not Available", "Code not running", "The current thread is not currently running code or the call stack could not be obtained" – transporter_room_3 May 05 '15 at 18:40
  • 1
    I'm curious how you access those action methods? – Win May 05 '15 at 18:53
  • http://localhost:port/api/employee/abc and http://localhost:port/api/employee – transporter_room_3 May 05 '15 at 19:02
  • @transporter_room Have you tried using IIS Express as the host? Just to try and isolate the problem. If you get it working, you'll know that something in your OWIN config is wrong. Also, don't be scared off about the breakpoint showing you the missing symbols, it should load at runtime. – Yuval Itzchakov May 05 '15 at 19:05
  • ***Both URLs go to GetEmployees() method.*** – Win May 05 '15 at 19:05
  • Win, I tried substituting all my controller logic with a single method from your example. I still get a 404. I commented your answer with screenshots. – transporter_room_3 May 05 '15 at 19:17
  • Yuval Itzchakov, I should be using IIS Express. The application code is running in the iisexpress.exe process according to Debug -> Windows -> Modules – transporter_room_3 May 05 '15 at 19:19

3 Answers3

2

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";
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • I don't think the route-"mapping" is off. It worked last night. But, I commented out my controller logic and replaced it with yours and this is the result: [first](http://i.imgur.com/Q4yltnj.png), [second](http://i.imgur.com/7YNWro8.png) – transporter_room_3 May 05 '15 at 19:09
  • Currently you have too many point of failures. You need to trace it back. Does your code work without OWIN? Besides, right click on solution and click `Clean Solution` just to make sure. – Win May 05 '15 at 19:16
  • You were right about the actions in the form that I posted them. I did remove some attribute routing before posting the controller code. However, even so the web api should respond with an exception message, which it does not. It still only replies with "404". I removed OWIN and the application now works. So the error must be with how I implemented OWIN support. – transporter_room_3 May 05 '15 at 20:09
0

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.

Andy Bullivent
  • 539
  • 1
  • 5
  • 8
0

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);          
    }
Mor
  • 1
  • 2