所请求的资源不支持HTTP方法“GET”。错误code 405 [英] The requested resource does not support http method 'GET'. Error Code 405

查看:2998
本文介绍了所请求的资源不支持HTTP方法“GET”。错误code 405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:现在固定的,显然API类不能是静态

我有一个Web API

 公共密封类DeploymentController:ApiController
{
 [HTTPGET]
公共静态的Htt presponseMessage获取([FromUri]参数配置)
{
  如果(部署== NULL)返回新的Htt presponseMessage(的HTTPStatus code.BadRequest);  DeploymentRepository.DetermineExtras(部署);
  HTT presponseMessage结果=新HTT presponseMessage(的HTTPStatus code.OK);
  result.Content =新StreamContent(DeploymentRepository.GenerateStreamFromString
    (DeploymentRepository.GetDeployment(部署)));
  result.Content.Headers.ContentType =
  新MediaTypeHeaderValue(应用程序/ CMD);
  result.Content.Headers.ContentDisposition =新ContentDispositionHeaderValue(附件)
  {
    文件名=DeployApplication.vbs
  };
  返回结果;
}

}

调用时,返回用于下载的文件

此API应该接受被称为链接用户点击。

如同

<$p$p><$c$c>http://localhost:52998/api/deployment?applicationname=something&systemkind=anotherthing&platformkind=Dotnet

但不是文件,我得到一个错误405。

 键入'System.Web.Http.Htt presponseException的第一次机会异常出现在System.Web.Http.dll
iisex press.exe信息:0:响应,状态= 405(MethodNotAllowed),方法= GET, Url=http://localhost:52998/api/deployment?applicationname=something&systemkind=anotherthing&platformkind=Dotnet,消息='内容类型=application / xml进行;字符集= UTF-8,内容长度=未知
iisex press.exe信息:0:操作= XmlMediaTypeFormatter.WriteToStreamAsync

我已经尝试过的方法建议similiar的问题,但至今没有成功。


解决方案

看来你还没有启用CORS。以下是使其CORS启用步骤。

步骤使其CORS启用:


  1. 安装本 - 安装封装用的NuGet Microsoft.AspNet.WebApi.Cors

  2. 打开文件 App_Start / WebApiConfig.cs 。以下code添加到 WebApiConfig.Register 方法。

  3. 接下来, [EnableCors] 属性添加到控制器类:

    通过以​​下PARAMS

    [EnableCors(来源:您的网域,标题:*,方法:GET)]


  4. 重新调整你的WebAPI项目。


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

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

Edit: Fixed now, apparently API classes cannot be static.

I have a 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;
}

}

that returns a file for download when invoked.

This API is supposed to get called by Links the User clicks on.

Like

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

But instead of the File, i get an Error 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

I have already tried the methods suggested to similiar questions, so far without success.

解决方案

It seems you have not enabled CORS. Following are the steps to make it CORS enabled.

Steps to make it CORS enabled:

  1. Install this - Install-Package Microsoft.AspNet.WebApi.Cors using NuGet
  2. Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.
  3. Next, add the [EnableCors] attribute to the Controller class:

    With following params

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

  4. Redeploy your WebAPI project.

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

More links - http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En

这篇关于所请求的资源不支持HTTP方法“GET”。错误code 405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆