修改引用到webapi的Dll文件对应的项目的web.config
选择生成读写方法webapi会生成四个读写的方法(CRUD),两个获取数据的、一个更新、一个删除,默认情况下更新和删除是不对外开外的,原因是安全问题,试想一下,可以删除数据,小A用户一次请求服务器所有的数据被删除了。但是可以通过简单的配置接收更新删除的请求,如下所示:
<system.webServer> <!--以下配置为了让IIS7+支持Put/Delete方法--> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol> <!--IIS7/7.5上必须加这个配置,否则访问报错--> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> <validation validateIntegratedModeConfiguration="false" /> <handlers> <remove name="WebDAV" /> <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" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_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" resourceType="Unspecified" requireAccess="Script" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_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" resourceType="Unspecified" requireAccess="Script" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers>
<!--提高GET URL长度限制上限--> <security> <requestFiltering> <requestLimits maxUrl="409600" maxQueryString="204800" /> </requestFiltering> </security>
</system.webServer>