Blog lập trình

Chia sẻ mọi kiến thức về lập trình

Enable PUT and DELETE Http verbs for WebAPI with Cors on IIS 8.5

 

Enable PUT and DELETE Http verbs for WebAPI with Cors on IIS 8.5

WebAPI in .Net by default allows only the GET, POST, OPTIONS and HEAD verbs. To allow PUT and DELETE, you need to remove the WebDAV handler and module from the request pipeline by making the following changes to the Web.Config.

The WebDAV element contains the settings that configure Web Distributed Authoring and Versioning (WebDAV) for Internet Information Services (IIS) 7 or above. WebDAV is an Internet-based open standard that enables editing Web sites over HTTP and HTTPS connections. You may get a 405 error code due to WebDAV.

<system.webServer>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://somedomain" />
        <add name="Access-Control-Expose-Headers" value="Content-Type, Accept, expiry, uid, access-token, token-type" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

Also, the ExtensionlessUrlHandler-Integrated-4.0 handler needs to allow all verbs with a * or a manual entry for specific verbs. The minimum runtime requirement is 4.0 to make this work.

The last bit to check for a message that says e.g. PUT /Rejected-By-UrlScan . Check the logs under C:\Windows\System32\inetsrv\urlscan\Logs on the Server. You may need to edit the config file to allow those verbs or remove the UrlScan 3.1 service in the ISAPI tab of your Service in IIS which points to urlscan.dll. This is usually mentioned last in the list.

CustomHeaders are part of enabling Cors. Please note that Access-Control-Allow-Origin should not allow “*” . It should mentions specific domain that requires to be allowed for Cors. You can expose custom headers using Access-Control-Expose-Headers and allow specific methods using Access-Control-Allow-Methods.

Show some love for the pit in my PayPal account.


PA CẤU HÌNH


<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <location path="." inheritInChildApplications="false">

    <system.webServer>

      <handlers>

                <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

                <remove name="TRACEVerbHandler" />

                <remove name="OPTIONSVerbHandler" />

                <remove name="WebDAV" />

        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />

                <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

    </handlers>

   <validation validateIntegratedModeConfiguration="false" />

    <modules>

      <remove name="WebDAVModule" />

    </modules>

  <httpProtocol>

      <customHeaders>

        <add name="Access-Control-Allow-Origin" value="http://crackieltswithrob.com/" />

        <add name="Access-Control-Expose-Headers" value="Content-Type, Accept, expiry, uid, access-token, token-type" />

        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />

      </customHeaders>

    </httpProtocol>

      <aspNetCore processPath="dotnet" arguments=".\IOITWebApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />

    </system.webServer>

  </location>

</configuration>

<!--ProjectGuid: 89A107A7-72B9-467E-A2D0-B194CD6FF82B-->

Share on Google Plus

About Blog chia sẻ kiến thức lập trình

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 nhận xét:

Đăng nhận xét