1

I have simple Web API controller in which GET and POST are working but PUT and DELETE is not working. Server throws The requested resource does not support http method 'PUT' error when accessing PUT and DELETE Here is how web.config looks like:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </configSections>
        <system.web>
            <compilation debug="true" targetFramework="4.5.2">
                <assemblies>
                    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                </assemblies>
            </compilation>
            <httpRuntime targetFramework="4.5.2" />
            <httpModules>
                <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
            </httpModules>
        </system.web>
        <system.webServer>
            <validation validateIntegratedModeConfiguration="false" />
            <modules>
                <remove name="ApplicationInsightsWebTracking" />
                <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
            </modules>
            <handlers>
                <remove name="ExtensionlessUrlHandler-Integrated-4.0" />      
                <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
            </handlers>
        </system.webServer>
        <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly>
                    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                </dependentAssembly>
                <dependentAssembly>
                    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
        <system.codedom>
            <compilers>
                <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
                <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
            </compilers>
        </system.codedom>    
    </configuration>

Code for Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

Code for Web API Configuration

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{Id}",
            defaults: new { Id = RouteParameter.Optional }
    );
}

And the controller has...

public class GroupsController : ApiController
{
    [System.Web.Http.HttpPut]
    public ActionResult Put(int Id, [FromBody]Group NewGroup)
    {
        //some code here
    }
}

I have also looked into online resource but none of those are helping out... here and here

I'm trying to make this API working in developer machine (IIS express/compact one that ships with Visual Studio).

Community
  • 1
  • 1
Hiren Desai
  • 941
  • 1
  • 9
  • 33

2 Answers2

4

It seems that you not pass id as url part. For example "put /api/Groups" will return this error, but "put /api/Groups/1" must work correctly.

Roman Patutin
  • 2,171
  • 4
  • 23
  • 27
  • Doing that throws different exception... { "Message": "No HTTP resource was found that matches the request URI 'http://localhost:21331/api/Groups/1'.", "MessageDetail": "No action was found on the controller 'Groups' that matches the request." } – Hiren Desai Jun 16 '16 at 12:00
  • Are you sure that you not reconfigure routing or not changed Id name in Action? Typically this error occurs when in routing optional parameter with one name but in controller name is different. For example routing configuration set Id parameter, but in action you set GroupId. For example with routing configuration that you posted action public ActionResult Put(int GroupId, [FromBody]Group NewGroup) will throw this error. – Roman Patutin Jun 16 '16 at 13:03
  • I installed help page nuget package and that helped me and the answer selected here is quite close to it. – Hiren Desai Jun 21 '16 at 04:11
0

Have you looked into the default route

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

routeTemplate: "api/{controller}/{action}/{id}",

The {action} is missing from your config.

and API, for reference :

[HttpPut]
public IHttpActionResult Hero(int Id)

The Below API Call works

http://localhost:48613/api/SuperHumans/Hero/1

The Below API Call WON'T work as the API route won't match

http://localhost:48613/api/SuperHumans/Hero

Also make sure you are passing correct content type from the client

EDIT -2

REST API

[System.Web.Http.HttpPut]
public void Put(int id, Dummy value)
{

}

Property Class

public class Dummy
{
    public string key1 { get; set; }
    public string key2 { get; set; }
}

POSTMAN REST CALL

POSTMAN REST CALL

Debugger

enter image description here

Farhan
  • 505
  • 5
  • 16
  • {action} is missing because that's how REST is supposed to work... A URL and HTTP VERB. Actions are not made publicly know. – Hiren Desai Jun 16 '16 at 13:21
  • I'm not accepting this answer because of a reason that {action} should not be part of your URL when designing REST services else it looses the conventions. – Hiren Desai Jun 21 '16 at 04:12