title: C# Web API 405 Error on GET
body: <p>I'm brand new to restful APIs after a decade on desktop development. I'm a little confused as to why I am getting a 405 attempting a GET for a controller.</p>
  
  <p>My controller:</p>
  
  <pre><code>public class ApplicantsController : ApiController
  {
  
      /// &lt;summary&gt;
      /// Gets the details of the applicant and their application
      /// &lt;/summary&gt;
      /// &lt;param name="applicantID"&gt;The ID of the applicant to get the most recent application and details for&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public HttpResponseMessage Get(int applicantID)
      {
          try
          {
              using (DbQuery query = new DbQuery("SELECT * FROM Applicants AS A WHERE A.ID = @ApplicantID",
                  new DbParam("@ApplicantID", applicantID)))
              {
                  using (DataTable data = query.ExecuteDataTable())
                  {
                      if (data.Rows.Count &gt; 0)
                      {
                          Applicant applicant = new Applicant(data.Rows[0]);
  
                          return new HttpResponseMessage()
                          {
                              Content = new StringContent(applicant.ToJson(), Encoding.UTF8, "text/html")
                          };
                      }
                  }
              }
  
              return new HttpResponseMessage(HttpStatusCode.NotFound);
          }
          catch (Exception ex)
          {
              Methods.ProcessException(ex);
              return new HttpResponseMessage(HttpStatusCode.InternalServerError);
          }
      }
  
      public HttpResponseMessage Post(Applicant applicant)
      {
          if (applicant.Save())
          {
              HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, applicant);
              string uri = Url.Link("DefaultApi", new { id = applicant.ID });
              response.Headers.Location = new Uri(uri);
  
              return response;
          }
  
          return Request.CreateResponse(HttpStatusCode.InternalServerError, "Error saving applicant");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have the same default routing in my WebApiConfig and confirmed that the way my controller is written it matches a standard Web API 2 controller with read, write, update methods. I've tried using DefaultAction, I've tried decorating methods with [HttpGet] and [AcceptVerbs]. Whenever I try to access the Get through either a browser myself or through ajax, I get a 405 (method not allowed).</p>
  
  <p>Ajax test:</p>
  
  <pre><code>        $("#TestGetApplicantButton").click(function (e) {
              e.preventDefault();
              alert("Getting Applicant...");
  
              $.ajax({
                  type: "GET",
                  url: "/api/Applicants/108",
                  contentType: "application/json; charset-utf-8",
                  dataType: "json",
                  success: function (data) {
                      $("#ResponseDiv").html(JSON.stringify(data));
                  },
                  failure: function (errMsg) {
                      alert(errMsg);
                  }
              });
          });
  </code></pre>
  
  <p>Ajax works perfectly for all of the other controllers, showing the data returned (example: <a href="https://i.stack.imgur.com/EL0k2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EL0k2.png" alt="Tests Work!"></a> and it even calls the Post method on <em>this</em> controller just fine. Yet I can't get my Get to work. I don't see where I could be going wrong.</p>
  
  <p>My routing:</p>
  
  <pre><code>    public static void Register(HttpConfiguration config)
      {
          // Web API configuration and services
  
          // Web API routes
          config.MapHttpAttributeRoutes();
  
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
  
          config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
      }
  </code></pre>
  
  <p>I've googled and checked here, but everyone seems to only have issues with POST, PUT, or DELETE, so I haven't found an answer for this. I've also tried removing the POST method in the controller - that gets me a 404 instead (not from my 404, I confirmed the code doesn't execute), which suggests for some reason the routing can't find my get method <em>at all</em>.</p>