Umair Malik
Umair Malik

Reputation: 27

"Message":"The requested resource does not support http method 'GET'." error

I have configured routing like this:

config.Routes.MapHttpRoute(
   name: "Sales",
   routeTemplate: "api/{Sales}/{jsonresponce}",
   defaults: new { controller = "Sales", action = "Postsomething" }
);

config.Routes.MapHttpRoute(
   name: "User",
   routeTemplate: "api/{User}/{GetDetails}",
   defaults: new { controller = "User", action = "GetDetails" }
);

Here is my UserController:

public class UserController : ApiController
{
    userservice objservice = new userservice();       

   [HttpGet]
   public CustDetails GetDetails(string Username, string Password, string BillingFeedID)
    {
        CustDetails model = new CustDetails();
       //checking for encrypted password
        model.UserName = Username;
        model.Password = Password;
        model.BillingFeedID = BillingFeedID;
        model = objservice.Login(model);

        //taking merchant configuration data
        var data = objservice.Getcustomerconfig(model.MerchantID, BillingFeedID);
        model.LastPosBillID = data.LastPosBillID;
        model.LastTimeStamp = data.LastTimeStamp;
        model.SyncStatus = data.SyncStatus;
        model.SynsTimeInterval = data.SynsTimeInterval;
        model.DataSorce = data.DataSorce;
        model.DataAuthentication = data.DataAuthentication;
        model.DataBaseQuery = data.DataBaseQuery;
        return model;        
    }
}

I also have a SalesController:

public class SalesController : ApiController
{

  [HttpPost]
  public async Task<HttpResponseMessage> PostSomething()
  {
      StringBuilder sb = new StringBuilder();
      try
      {
          string jsonData = await Request.Content.ReadAsStringAsync();
          // dynamic dataList = JArray.Parse(jsonData);             
          if (File.Exists(@"C:\MCarrots\Umairbills\Umairbills.json"))
              File.Delete(@"C:\MCarrots\Umairbills\Umairbills.json");
          File.Create(@"C:\MCarrots\Umairbills\Umairbills.json").Close();
          File.WriteAllText(@"C:\MCarrots\Umairbills\Umairbills.json", jsonData);           
          return Request.CreateResponse(HttpStatusCode.OK, "OK");
      }
      catch (Exception ex)
      {
          File.WriteAllText(@"C:\MCarrots\mcarrots\Umairbills.json", ex.ToString());
          return Request.CreateResponse(HttpStatusCode.NoContent, ex.ToString());
      }
  }

When I try to call the GetUserDetails action with this url:

http://localhost:42945/api/User/GetDetails?Username=kay001&Password=kay501&BillingFeedID=KF1

It is throwing this error:

"Message":"The requested resource does not support http method"

But the POST method in SalesController is working.

Upvotes: 1

Views: 585

Answers (1)

juunas
juunas

Reputation: 58733

Your route templates seem off. I think they should be:

    config.Routes.MapHttpRoute(
       name: "Sales",
       routeTemplate: "api/Sales/{action}",
       defaults: new { controller = "Sales", action = "Postsomething" }
    );

    config.Routes.MapHttpRoute(
          name: "User",
          routeTemplate: "api/User/{action}",
          defaults: new { controller = "User", action = "GetDetails" }
       );

I changed the route templates so that the controller name is essentially hard-coded, and the action is a placeholder. The action can be left out in this case though, defaulting to GetDetails.

Upvotes: 3

Related Questions