Web Api 调用时出现405错误解决方案

Web Api 调用时出现405错误
之前可以调用成功,说明配置没问题

说明: .net core WebApi 在配置终结点时adress不需要指定svc,但是wcf服务需要

错误提示:
System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)
--- End of stack trace from previous location where exception was thrown ---
   at Tuhu.Service.TuhuServiceClient`1.InvokeAsync[T](Func`2 func, CancellationToken cancellationToken)

解决方案

配置终结点地址时配置完整路径,而不是只配置前缀

来源:
You have to specify full address for your service in the endpoint tag of Client configuration

address="http://localhost:8002/HelloWorldService.svc", where you put same endpoint configurations.

It did work for me while I trapped in hell...

https://stackoverflow.com/questions/18004206/protocolexception-unhandled-405-method-not-allowed-with-wcf-bindings-and-endp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTTP 405错误通常发生在使用Ajax调用Web API,这是因为Web API默认情况下只支持GET和POST请求,而不支持PUT、DELETE、OPTIONS等请求方法。解决这个问题的方法是在Web API端添加一个处理OPTIONS请求的方法。 在Web API中,可以使用Web API的CORS功能来解决这个问题。CORS(跨域资源共享)是一种Web标准,允许在不同域名下的浏览器访问服务器资源。 为了启用CORS,需要在Web API项目中安装Microsoft.AspNet.WebApi.Cors NuGet包。然后,在WebApiConfig类中启用CORS: ``` using System.Web.Http; using System.Web.Http.Cors; public static class WebApiConfig { public static void Register(HttpConfiguration config) { // 启用CORS var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); // ...其他配置 } } ``` 这里的“*”表示允许任何域名、任何方法、任何标头访问Web API。你也可以指定具体的域名、方法和标头。 在客户端使用Ajax,需要设置withCredentials为true,这样才能在跨域请求发送Cookies和认证信息。例如: ``` $.ajax({ type: "POST", url: "http://example.com/api/resource", data: JSON.stringify({data: "hello"}), contentType: "application/json", xhrFields: { withCredentials: true }, success: function(data) { console.log(data); }, error: function(xhr, status, error) { console.log(xhr.responseText); } }); ``` 在这个例子中,我们向http://example.com/api/resource发送一个带有数据的POST请求,并设置withCredentials为true,以便发送Cookies和认证信息。如果请求成功,我们将收到响应数据,并将其打印到控制台中。如果请求失败,我们将打印xhr.responseText,这包含了服务器返回的错误信息。 希望这个解决方案可以帮助你解决问题!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值