naffie
naffie

Reputation: 729

Azure App Service Error 405 - The request could not be completed. (Method Not Allowed)

I'm working with Azure App Service .NET backend with a Xamarin.iOS app. I am able to successfully register a new user and I can see the user's details in the database. I have a custom ApiController, which handles the registration and the I'm able to save the details with a successful POST call.

However, when I try to log into the app, I get the following error:

{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed.  (Method Not Allowed) 

Below is my code:

The RegistrationController in the backend which successfully makes a POST call

[MobileAppController]
[RoutePrefix("api/register")]
[AllowAnonymous]
public class RegisterController : ApiController
{

     [HttpPost]
    [Route("newuser")]
    public HttpResponseMessage NewUser(RegistrationRequest request)
    {
        // Registration code in here

    }

}

This is how I call this function on the client side:

public async Task<Result<UserProfile>> RegisterUser(RegistrationWrapper registrationrequest)
    {
        try
        {
            var registrationRequest = new JObject();
            registrationRequest.Add("username", registrationrequest.username);
            registrationRequest.Add("password", registrationrequest.password);
            registrationRequest.Add("email", registrationrequest.email);
            registrationRequest.Add("phone", registrationrequest.phone);
            registrationRequest.Add("firstname", registrationrequest.firstname);
            registrationRequest.Add("lastname", registrationrequest.lastname);

            var result = await client.InvokeApiAsync("register/newuser", registrationRequest);


     // Handle result here

        }
        catch (Exception ex)
        {
            return Result<UserProfile>.Failure(ex.Message + ex.StackTrace + ex.InnerException);
        }

    } 

Custom AuthController which handles the login

This POST call fails with the error described above.

 [MobileAppController]
[RoutePrefix("api/auth")]
public class AuthController : ApiController
{
    public HttpResponseMessage Post(AuthenticationRequest credentials)
    {
        try
        {
              //Authentication code goes here

          catch (Exception e)
        {
            Console.WriteLine("Ërror :" + e.Message);
            Console.WriteLine(e.StackTrace);
            return Request.CreateResponse(HttpStatusCode.InternalServerError, new
            {
                Stacktrace = e.StackTrace,
                ErrorMessage = e.Message,
                Credentials = credentials
            });
        }


    }

How I invoke this function from the client side

 async Task<Result<Account>> Login(string username, string password)
    {
        try
        {
            var credentials = new JObject();
            credentials.Add("username", username);
            credentials.Add("password", password);
            var result = await client.InvokeApiAsync("auth", credentials);


          //Handle result here

        }
        catch (Exception ex)
        {
            return Result<Account>.Failure(ex, ex.Message + ex.StackTrace);
        }

    }

}

I'm not sure why it's failing during the log in. Any ideas?

Upvotes: 3

Views: 7159

Answers (2)

Martin Staufcik
Martin Staufcik

Reputation: 9490

The HTTP status code 405 is returned when an API endpoint is called with a wrong (Not Allowed) HTTP method. For example, if instead of a POST request the endpoint is called with a GET request.

Upvotes: 0

naffie
naffie

Reputation: 729

After trying tons of solutions found here on StackOverflow, the one that finally worked for me was this first answer found on a similar question.

It seems that the http POST call is redirected to https.

After enabling authentication on your App Service in the Azure portal, you need to change the url to https.

So I changed mine from this:

http//{my_site}.azurewebsites.net

To this:

https//{my_site}.azurewebsites.net

On the client side, and now used this new one to create my local sync tables. Everything works as expected now.

Upvotes: 6

Related Questions