Bartosz
Bartosz

Reputation: 4592

The remote server returned an error: (405) Method Not Allowed

I was trying to research the problem, but failed, therefore am asking this question here. I have an MVC application calling Web API. One of the methods returns 405 error, and I have no idea why, especially that all of the others methods in the same controller work absolutely fine.

This is how I call this method from MVC end:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}/api/Account/ArchiveAddress?addressId={1}", uri, addressId));
request.Method = "Get";

try
{
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch (Exception e)
{
     return RedirectToAction("Error", "Error", new { error = e.Message });
}

Where uri is a string:

http://localhost:52599

and addressId is an int

My method on the Web API end looks like this:

[Route("ArchiveAddress")]
public IHttpActionResult ArchiveUserAddress(int addressId)
{
    var address = _addressRepo.Find(addressId);
    ...

As I said, I call many different methods on the API in the exact same way, and they all work fine. Just this one does not want to behave. What might be causing that?

Upvotes: 1

Views: 9542

Answers (1)

djikay
djikay

Reputation: 10628

I think you need to decorate your action method (ArchiveUserAddress) with the [HttpGet] attribute or name it something that begins with Get..., e.g. GetArchiveUserAddress. As it stands, it would do match the POST method which means there's no getter hence the error you're getting.

Upvotes: 1

Related Questions