The requested resource does not support http method 'GET'. Error Code 405

混江龙づ霸主 提交于 2019-12-13 03:59:58

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/26487126/the-requested-resource-does-not-support-http-method-get-error-code-405

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!