0

On StackOverflow I saw a lot of people suggesting to change my web.config, so I changed it to this:

<modules>
  <remove name="FormsAuthentication" />
  <remove name="WebDAVModule" /> 
</modules>
<handlers> 
  <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

Yet I am still having the same problems as before. Any other suggestions? the method I am posting to is just a simple put

var response = await client.PutAsJsonAsync($"api/{user.Email}/", user);

In the API the method it's calling looks like this:

[HttpPut]
public void Account(string userName, JObject user)
{
    ....
}

Here is the route config

config.Routes.MapHttpRoute( 
    name: "IdentityApi", 
    routeTemplate: "{userName}/{action}/", 
    defaults: new { controller = "Identity", action = "Account" } 
);
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Arn Vanhoutte
  • 1,779
  • 4
  • 16
  • 28
  • Mayby this will help http://stackoverflow.com/questions/38207153/error-405-method-not-allow-when-calling-put-method-in-postman-with-body-parame/38207759#38207759 – Marcus Höglund Jul 15 '16 at 13:22
  • there is probably a way to do this without HttpPut anyway, but i'd love to see the answer too – Antoine Pelletier Jul 15 '16 at 13:23
  • Not sure how you `JObject` would work? How does it now to convert a user (as JSON) to JObject? Try getting down to basic by replacing `JObject user` with `string user` and then call the service with `client.PutAsJsonAsync($"api/{user.Email}/", "testUserName")` – Michael Jul 15 '16 at 13:25
  • Also, could you update you question with you routing details – Michael Jul 15 '16 at 13:25
  • 1
    As per @AntoinePelletier comment, there is actually no difference on HttpPut and HttpPost. It is a question about semantic. See [PUT vs POST](http://restcookbook.com/HTTP%20Methods/put-vs-post/) – Michael Jul 15 '16 at 13:28
  • I tried @Michael's suggestion but it still does not work. It keeps returning the 405 code – Arn Vanhoutte Jul 15 '16 at 13:40
  • When I'm changing it to a post (so PostAsJsonAsync and HttpPost) I'm getting a 404 error – Arn Vanhoutte Jul 15 '16 at 13:44
  • Please add your `MapHttpRoute` to your question – Michael Jul 15 '16 at 13:53
  • Do you mean this: `config.Routes.MapHttpRoute( name: "IdentityApi", routeTemplate: "{userName}/{action}/", defaults: new { controller = "Identity", action = "Account" } );` – Arn Vanhoutte Jul 15 '16 at 14:03
  • Really just guessing here, but aren't you missing api from the routeTemplate: `"api/{userName}/{action}/"`? – Michael Jul 16 '16 at 18:11

2 Answers2

0

Based on your route template routeTemplate: "{userName}/{action}/" you are calling the wrong URL api/{user.Email}/.

The assumption here is that your ApiController looks like this

public class IdentityController : ApiController {
    [HttpPut]
    public void Account(string userName, JObject user) { .... }
}

You should update either your route template to match the URL you want

config.Routes.MapHttpRoute( 
    name: "IdentityApi", 
    routeTemplate: "api/{userName}", 
    defaults: new { controller = "Identity", action = "Account" } 
);

or the URL you are calling to match your original routeTemplate...

var response = await client.PutAsJsonAsync($"{user.Email}/", user);
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

Can you try like this in your web.config:

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
Bhupinder Singh
  • 254
  • 2
  • 15