【问题标题】:The requested resource does not support http method 'GET'. Error Code 405请求的资源不支持 http 方法“GET”。错误代码 405
【发布时间】:2014-12-16 16:52:30
【问题描述】:

编辑:现在已修复,显然 API 类不能是静态的。

我有一个 Web API

 public sealed class DeploymentController : ApiController
{
 [HttpGet]
public static HttpResponseMessage Get([FromUri]Parameters deployment)
{
  if (deployment == null) return new HttpResponseMessage(HttpStatusCode.BadRequest);

  DeploymentRepository.DetermineExtras(deployment);
  HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
  result.Content = new StreamContent(DeploymentRepository.GenerateStreamFromString
    (DeploymentRepository.GetDeployment(deployment)));
  result.Content.Headers.ContentType =
  new MediaTypeHeaderValue("application/cmd");
  result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  {
    FileName = "DeployApplication.vbs"
  };
  return result;
}

}

调用时返回一个文件以供下载。

这个 API 应该被用户点击的链接调用。

喜欢

http://localhost:52998/api/deployment?applicationname=something&systemkind=anotherthing&platformkind=Dotnet

但不是文件,而是错误 405。

A first chance exception of type 'System.Web.Http.HttpResponseException' occurred in System.Web.Http.dll
iisexpress.exe Information: 0 : Response, Status=405 (MethodNotAllowed), Method=GET, Url=http://localhost:52998/api/deployment?applicationname=something&systemkind=anotherthing&platformkind=Dotnet, Message='Content-type='application/xml; charset=utf-8', content-length=unknown'
iisexpress.exe Information: 0 : Operation=XmlMediaTypeFormatter.WriteToStreamAsync

我已经尝试过类似问题的建议方法,到目前为止没有成功。

【问题讨论】:

  • 你在 webapi 中启用了 CORS 了吗?

标签: html asp.net iis-express http-status-code-405


【解决方案1】:

您似乎没有启用 CORS。以下是启用 CORS 的步骤。

启用 CORS 的步骤:

  1. 安装这个 - Install-Package Microsoft.AspNet.WebApi.Cors using NuGet
  2. 打开文件App_Start/WebApiConfig.cs。将以下代码添加到 WebApiConfig.Register 方法中。
  3. 接下来,将[EnableCors] 属性添加到Controller 类:

    有以下参数

    [EnableCors(origins: "your_domain", headers: "*", methods: "GET")]

  4. 重新部署您的 WebAPI 项目。

来源 - http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

更多链接 - http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En

【讨论】:

    猜你喜欢
    • 2017-12-15
    • 2012-09-27
    • 2014-01-28
    • 1970-01-01
    • 2015-10-08
    • 2014-06-23
    • 2017-05-24
    • 2015-04-23
    • 2016-11-18
    相关资源
    最近更新 更多