添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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;
												

Hello @Ying Kit Li
I would suggest to add the correct tag for you questions, so the specific specialist could find it.

azure-api-management:
Technical questions about Azure API Management, a hybrid, multi-cloud management platform for APIs across all environments.

azure-webapps:
Technical questions about Azure Web Apps, which helps you build, deploy, and scale enterprise-grade web and API apps running on Windows or Linux, using code or containers, written with frameworks including .NET, .NET Core, Java, Node.js, Python, and PHP.

azure-webapps-apis:
Technical questions about Azure Webapps APIs, API features of the Azure Web App product. Please note that this does not include Technical questions about APIs in Azure.

azure-webapps-development:
Technical questions about development of your site for Azure Web Apps.

Best Regards
Karlie

----------

If the Answer is helpful, please click "Accept Answer" and upvote it.

@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.

Category: Microsoft.EntityFrameworkCore.Database.Command
EventId: 20102
SpanId: 72a14d84e3dafc48
TraceId: 2c34610f15ac434b8ca8e358c4de818a
ParentId: 0000000000000000
RequestId: 80001439-0002-b400-b63f-84710c7967bb
RequestPath: /Password
ActionId: 3d47aaba-8fa6-4b3a-8104-ab123904eeb5
ActionName: WebAPI.Controllers.PasswordController.UpdatePassword (WebAPI)

Failed executing DbCommand (252ms) [Parameters=[pMobileNo='?', pPassword='?'], CommandType='Text', CommandTimeout='30']
EXECUTE dbo.uspPasswordSalt @pMobileNo,@pPassword

It is strange but everythings work found after it added [AcceptVerbs("GET")] to the code:

https://datingbirdapi.azurewebsites.net/Password/91111111/abcd1234
https://datingbirdapi.azurewebsites.net/Login/91111111/abcd1234