0

I'm going through a book to learn more about MVC's WebAPI controllers, and have become stuck on an issue. I'm trying to POST to my WebAPI controller (in Visual Studio 2010) using Fiddler, but am unable to get the connection to my controller.

I have the following class that I'm using per the book:

public class Employee {
    public int      Id          { get; set; }
    public string   FirstName   { get; set; }
    public string   LastName    { get; set; }
    public int      Department  { get; set; }
}

My WebApi Controller POST method, with a breakpoint on the first var maxId = ... line:

public class EmployeesController : ApiController {
    public HttpResponseMessage Post(Employee employee) {
        var maxId = list.Max(e => e.Id);
        employee.Id = maxId + 1;

        list.Add(employee);

        var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, employee);

        string uri = Url.Link("DefaultApi", new { id = employee.Id });
        response.Headers.Location = new Uri(uri);
        return response;
    }
}

I'm able to post a GET through Fiddler2 without issue (rerunning the request that I track from Chrome:

Successful GET

I then tried adjusting the headers to run a POST, and received a 404 while using "localhost" as my server:

404 localhost

Per many suggestions, I tried changing the server to "localhost.", but received the same 404:

404 localhost.

I tried one last time with changing from "localhost" to my computer name, and received a 502 instead after it tried to make the post for a second or so (this had the same content as the previous two requests, but only with the above named change to the url):

502 frankgrimes

Per some other recommendations, both in the book and online, I had also added the following to my Web.config in the <configuration> node before starting on trying any of these POSTS. This resides immediately after the <configSections> node.

<system.net>
  <defaultProxy>
    <proxy usesystemdefault="False" bypassonlocal="True" proxyaddress="http://127.0.0.1:8888"/>
  </defaultProxy>
</system.net>

Any ideas of what I'm missing? So far in all of my research, this StackOverflow question has been the closest to helping me, but I had already tried the recommendations of the only answer there, and even the video gave me no further insight.

At this point, I would not be surprised if part of it has to do with Visual Studio 2010, unless I've missed something completely simple.

Community
  • 1
  • 1
krillgar
  • 12,596
  • 6
  • 50
  • 86
  • Your port looks like one used by the .NET debugger with IIS Express, have you stopped debugging? You might want to set it up directly with IIS so it is always there. Right click on your web project, view properties, and look at the 'Web' section. – jtimperley Jul 30 '14 at 13:31
  • Yes, I was just using the default debugger. I followed your lead and switched from "Use Visual Studio Development Server" to "Use Local IIS Web Server", and left everything there as the defaults. Was that what you were referring to? – krillgar Jul 30 '14 at 13:34
  • Did that resolve your issue? I believe that should automatically register your application with the local IIS when the solution is opened. Basically, what I intended was with IIS Express if often only is available when Visual Studio is bound to it, so if you stopped debugging you'd receive that 404 because there is nothing registered to that endpoint anymore. With the local IIS it is always available and Visual Studio will automatically bind to the w3p.exe process when you need to debug. – jtimperley Jul 30 '14 at 13:47
  • No, I would leave my application running as I was trying to use Fiddler. I knew that would immediately fail if I didn't. I tried the settings I described above, and on trying to run without debugging, I get an error in my browser of "500 - Internal Server Error Calling LoadLibraryEx on ISAPI filter "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" failed". I touched no other settings. – krillgar Jul 30 '14 at 13:53
  • You don't need to do anything with your proxy settings to use Fiddler's Composer. You don't need to use `localhost.` or the machine name either; these are all hacks that are needed in some cases for proxying, but you're not using the proxy in this scenario at all. Doesn't WebAPI require that you have some sort of annotation to properly route POST to a given method? – EricLaw Jul 31 '14 at 12:25

1 Answers1

-1

Are you sure you have the default routing defined? It should look like this:

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
  • Yes, I do have it defined in my WebApiConfig (so `config.Routes.MapHttpRoute` as opposed to just `routes.MappHttpRoute`), and if I didn't then the calls directly from the browser and the recall from Fiddler (the first picture) would have failed as well. – krillgar Jul 31 '14 at 12:00
  • Not true. You *can* have multiple routings, and you *can* rewrite the default routing keeping the name. Anyway, which Web API version are you using? – Arthur Peka Jul 31 '14 at 12:10
  • Yes, I know you can have multiple routes defined, however the one you shared is the only one that I have. I'm using the Web API that is a part of MVC 4. – krillgar Aug 05 '14 at 15:04