问题
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:
- Install this -
Install-Package Microsoft.AspNet.WebApi.Cors using NuGet
- Open the file
App_Start/WebApiConfig.cs
. Add the following code to theWebApiConfig.Register
method. Next, add the
[EnableCors]
attribute to the Controller class:With following params
[EnableCors(origins: "your_domain", headers: "*", methods: "GET")]
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