转载

外部调用C#一般处理程序返回405 method not allowed解决办法

在做跨域接口调用的时候,写了个handler,在本地部署好了之后测试是可以用的,但是供外部链接调用的时候返回了405 method not allowed,解决办法如下:

<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol> <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" 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" /> <add name="handler" path="handler.ashx" verb="*" type="WebApp.handler"/> <remove name="OPTIONSVerbHandler" /> </handlers> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> </system.webServer>

注意:

上述配置中, 青色部分是结决跨域问题红色部分是解决405错误的,

尤其要注意 加粗部分 ,添加对应的handler文件,后面的type是"命名空间.类名",如

namespace WebApp

{

/// <summary>

/// handler 的摘要说明

/// </summary>

public class handler : IHttpHandler

{

...

}

正文到此结束
Loading...