码迷,mamicode.com
首页 > Windows程序 > 详细

IIS不支持WebAPI Put和Delete,解决方法

时间:2019-03-25 17:44:21      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:sse   ida   bpa   iis   framework   newton   添加   UNC   集成   

环境windows10+IIS 10

技术图片

把 托管模式 设置为 集成

技术图片

web.config设置如下

<?xml version="1.0" encoding="UTF-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <connectionStrings>
    <add name="db" connectionString="server=.;userid=sa;password=;database=Test;" />
  </connectionStrings>
  <!--
    有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。

    可在 <httpRuntime> 标记上设置以下特性。
      <system.Web>
        <httpRuntime targetFramework="4.5" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime />
    <pages controlRenderingCompatibilityVersion="4.0" />
  </system.web>
  <system.webServer>
    <handlers>
      <remove name="ISAPI-dll" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="ISAPI-dll" path="*.dll" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="File" requireAccess="Execute" allowPathInfo="true" preCondition="bitness32" />
    </handlers>
    <httpProtocol>
      <customHeaders>
        <!-- 先移除后添加,确保起作用 -->
        <remove name="Access-Control-Allow-Origin" />
        <remove name="Access-Control-Allow-Headers" />
        <remove name="Access-Control-Allow-Methods" />
        <remove name="Access-Control-Max-Age" />
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Max-Age" value="1728000" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="TelemetryCorrelationHttpModule" />
      <remove name="WebDAVModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
    </modules>
        <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="iisstart.htm" />
            </files>
        </defaultDocument>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.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=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebAPI.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet]
        public string Get(int id)
        {
            return "Get,  "+id.ToString();
        }

        // POST api/values
        [HttpPost]
        public string Post([FromBody]string value)
        {
            return "Post,  " + "";
        }

        // PUT api/values/5
        [HttpPut]
        public string Put(int id, [FromBody]person p)
        {
            return "Put,  " + p.id.ToString()+","+p.name.ToString();
        }

        // DELETE api/values/5
        [HttpDelete]
        public string Delete(int id)
        {
            return "Delete,  " + id.ToString();
        }
    }

    public class person
    {
        public string id { get; set; }
        public string name { get; set; }
    }
}

前端页面代码:

技术图片
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>TestAPI</title>
    <style>
        
        li a { cursor:pointer; line-height:200%;}
    </style>
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        function CallGet() {
            //alert("Get");
            $.ajax({
                type: "Get",
                url: "/api/Values",
                data: { id: "666" },
                success: function (data) {
                    $("#con").append("<br />Get到的数据为:"+data);

                }
            })

        }
        function CallPost() {
            $.ajax({
                type: "Post",
                url: "/api/Values",
                data: { "id": "B3D3D6C7-4CC8-410A-A6D6-58CBAC26D2B8", "name": "TestName" },
                success: function (data) {
                    
                    $("#con").append("<br />Post到的数据为:" + data);
                }
            });
            
        }
        function CallPut() {
            $.ajax({
                type: "Put",
                url: "/api/Values?id=7",
                data: { id:1111,name: "TestName" },
                success: function (data) {
                    $("#con").append("<br />Put到的数据为:" + data);
                }
            });
        }
        function CallDelete() {
            $.ajax({
                type: "Delete",
                url: "/api/Values?id=888",
                data: { id: 888 },
                success: function (data) {
                    $("#con").append("<br />Delete到的数据为:" + data);
                }
            });
        }

        function CallReq() {
            $.ajax({
                type: "get",
                url: "/home/TestReques",
                success: function (data) {
                    $("#con").append("<br />后台调用的Put结果是:"+data);
                }
            });
        }
    </script>
</head>
<body>
    <div> 
        <ul>
            <li><a onclick="CallGet()">Get请求</a></li>
            <li><a onclick="CallPost()">Post请求</a></li>
            <li><a onclick="CallPut()">Put请求</a></li>
            <li><a onclick="CallDelete()">Delete请求</a></li>
            <li><a onclick="CallReq()">CallReq</a></li>
        </ul>
    </div>
    <div id="con">

    </div>
</body>
</html>
View Code

 

IIS不支持WebAPI Put和Delete,解决方法

标签:sse   ida   bpa   iis   framework   newton   添加   UNC   集成   

原文地址:https://www.cnblogs.com/wehas/p/10595002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!