【问题标题】:HTTP PUT is not working in ASP.NET Web APIHTTP PUT 在 ASP.NET Web API 中不起作用
【发布时间】:2016-06-16 10:32:58
【问题描述】:

我有一个简单的 Web API 控制器,其中 GET 和 POST 正在工作,但 PUT 和 DELETE 不工作。服务器在访问 PUT 和 DELETE 时抛出 The requested resource does not support http method 'PUT' 错误 web.config 如下所示:

<?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>

Global.asax 代码

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

Web API 配置代码

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

控制器有...

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

我也查看了在线资源,但这些都没有帮助...... here 和 here

我正在尝试让这个 API 在开发者机器上工作(IIS express/compact 随 Visual Studio 一起提供)。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-web-api asp.net-web-api2 asp.net-web-api-routing


    【解决方案1】:

    您似乎没有将 id 作为 url 部分传递。例如“put /api/Groups”会返回此错误,但“put /api/Groups/1”必须正常工作。

    【讨论】:

    • 这样做会引发不同的异常... { "Message": "未找到与请求 URI 'localhost:21331/api/Groups/1'." 匹配的 HTTP 资源。", "MessageDetail": "在与请求匹配的控制器“组”。” }
    • 您确定您没有重新配置路由或没有更改操作中的 ID 名称吗?通常,当路由可选参数具有一个名称但控制器名称不同时,会发生此错误。例如路由配置设置 Id 参数,但实际上您设置了 GroupId。例如,使用您发布操作 public ActionResult Put(int GroupId, [FromBody]Group NewGroup) 的路由配置将引发此错误。
    • 我安装了帮助页面 nuget 包,这对我有帮助,这里选择的答案非常接近。
    【解决方案2】:

    你看过默认路由吗

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

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

    您的配置中缺少 {action}。

    和API,供参考:

    [HttpPut]
    public IHttpActionResult Hero(int Id)
    

    以下 API 调用有效

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

    下面的 API 调用不起作用,因为 API 路由不匹配

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

    还要确保从客户端传递正确的内容类型

    编辑-2

    REST API

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

    属性类

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

    邮递员休息电话

    调试器

    【讨论】:

    • {action} 丢失了,因为这就是 REST 应该如何工作的......一个 URL 和 HTTP 动词。行动不公开。
    • 我不接受这个答案,因为在设计 REST 服务时 {action} 不应成为您的 URL 的一部分,否则它会失去约定。
    猜你喜欢
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多