405 error of web api in azure

Ying Kit Li 1 Reputation point
2021-05-20T17:22:27.76+00:00

I would like to ask about 405 error of web api in azure? recently I create some httpput/ httppost web api services which use stored procedure (fromSqlRaw) and they are deployed to my azure web api path. But all faced 405 error e.g. (https://datingbirdapi.azurewebsites.net/Password/97777777/abcd1234)
Did azure not support restful service that use stored procedure for data manipulation? Below are two api service for password update and login:

[Route("{Contact_No}/{Password}")]
[HttpPut]
public ActionResult<List<passwordDTO>> UpdatePassword(string Contact_No, String Password)
{
    var mobileNo = new SqlParameter("pMobileNo", Contact_No);
    var password = new SqlParameter("pPassword", Password);

    var passwordReturn = _context.Customer.FromSqlRaw("EXECUTE dbo.uspPasswordSalt @pMobileNo,@pPassword", mobileNo, password).ToList();
    return  passwordReturn;
}


[HttpGet]
public ActionResult<List<Login>> Login(String Contact_No, String Password)
{
    var mobileNo = new SqlParameter("pMobileNo", Contact_No);
    var password = new SqlParameter("pPassword", Password);

    var LoginDetails = _context.Customer.FromSqlRaw("EXECUTE dbo.uspLogin @pMobileNo,@pPassword", mobileNo, password).ToList();

    if (LoginDetails == null)
    {
        return NotFound();
    }

    return LoginDetails;

}
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,981 Reputation points Microsoft Employee
    2021-05-25T13:16:11.817+00:00

    @Ying Kit Li , Azure definitely supports restful services. I would suggest enabling application logging so you can see what exactly is causing your 405 error. Another place to check is the Diagnose and solve problems blade.

    Having said that, a 405 error usually means method not allowed, therefore, I would make sure the client is calling UpdatePassword method route with HTTP PUT verb. I would also wrap passwordReturn in an OkObjectResult and wrap the .FromSqlRaw command in a try catch so BadRequest could be returned in case the stored procedure failed to execute.

    EDIT:

    Azure doesn't restrict how your app responds to request, you control that. In your code snippet:

       [Route("{Contact_No}/{Password}")]  
       [HttpPut]  
       public ActionResult<List<passwordDTO>> UpdatePassword(string Contact_No, String Password)  
    

    [HttpPut] will mean your <yourapp>.azurewebsites.net/{Contact_No}/{Password} will only respond to HTTP PUT request. If you want this method to also respond to GETs, then decorate your method by adding [HttpGet] as well. You can also use [AcceptVerbs()] as you previously discovered.

    So your method could easily be

       [Route("{Contact_No}/{Password}")]  
       [HttpGet]  
       [HttpPut]  
       public ActionResult<List<passwordDTO>> UpdatePassword(string Contact_No, String Password)  
    

    It is one and the same as doing

       [Route("{Contact_No}/{Password}")]  
       [HttpPut]  
       [AcceptVerbs("GET")]  
       public ActionResult<List<passwordDTO>> UpdatePassword(string Contact_No, String Password)  
    

    If dbo.uspPasswordSalt isn't really doing any data updates and is simply retrieving data, then I would recommend removing [HttpPut] attribute as PUT infers the API method will insert (or update) that resource.

    See https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api#routing-variations for more information.