当方法具有变量MVC 4时,WebApi PUT不起作用

时间:2014-01-28 23:45:06

标签: asp.net-mvc-4 c#-4.0 soap asp.net-web-api webmethod

我是WebApi的新手,教程进展顺利。我得到了Get,Get变量,Post和Delete来工作,但我似乎无法让Put工作。好吧,如果我从控制器中的方法中删除所有变量,它实际上会进入方法,但这很无用。有没有人在知道如何解决之前遇到过这个问题?

PUT Url发给邮递员:

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

预览:

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

错误讯息:

 {"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."}

控制器:

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>

1 个答案:

答案 0 :(得分:2)

将您的操作更改为Put(string id)Put([FromUri(Name="id")]string guid)