Question

I'm new to WebApi and the tutorial was going well. I got Get, Get with variables, Post, and Delete to work, but I can't seem to get Put to work. Well, if I remove all the variables from the method the in the controller, it actually goes into the method, but that's pretty useless. Has anyone come across this before that knows how to fix it?

PUT Url sent to Postman:

http://localhost:60679/api/incident/1234

Preview:

PUT /api/incident/1234 HTTP/1.1
Host: localhost:60679
Cache-Control: no-cache

Error Message:

 {"Message":"No HTTP resource was found that matches the request URI     
'http://localhost:60679/api/incident/1234'.","MessageDetail":"No action was found on the      
controller 'Incident' that matches the request."}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApiTest.AppLogic;
using WebApiTest.Data.Core;

namespace WebApiTest.WebApi.Controllers
{
public class IncidentController : ApiController
{


    // GET api/values
    public IEnumerable<Incident> Get()
    {
        using (EntityDemoEntities context = new EntityDemoEntities())
        {
            return context.Incidents.ToList<Incident>();
        }
    }

    // GET api/values/5
    public Incident Get(string id)
    {
        using (EntityDemoEntities context = new EntityDemoEntities())
        {
            Guid tempGuid = new Guid(id);
            return context.Incidents.SingleOrDefault(i => i.IncidentId == tempGuid);
        }
    }


    // PUT api/values/5
    [HttpPut]
    public void Put(string guid)
    {
        HttpResponseMessage result = new HttpResponseMessage();
        try
        {
            if (ModelState.IsValid)
            {
                //Do something
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.InternalServerError, "Invalid Model");
            }
        }
        catch (Exception ex)
        {
            result = Request.CreateResponse(HttpStatusCode.InternalServerError, String.Format("{1}{0}{2}{0}{3}{0}{4}", Environment.NewLine, ex.Message, ex.StackTrace, ex.InnerException == null ? String.Empty : ex.InnerException.Message, ex.InnerException == null ? String.Empty : ex.InnerException.StackTrace));
        }
    }
}
}

WebApiConfig

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

    }
}

Web.config

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name ="WebDAVModule"/>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <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>
<modules runAllManagedModulesForAllRequests="true">
  <remove name='WebDAVModule'/>
</modules>

Was it helpful?

Solution

Change your action to Put(string id) or Put([FromUri(Name="id")]string guid)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top