编辑:现在修复,显然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
到目前为止,我已经尝试过建议用于类似问题的方法。
答案 0 :(得分:0)
好像你没有启用CORS。以下是启用CORS的步骤。
启用CORS的步骤:
Install-Package Microsoft.AspNet.WebApi.Cors using NuGet
App_Start/WebApiConfig.cs
。将以下代码添加到WebApiConfig.Register
方法。接下来,将[EnableCors]
属性添加到Controller类:
以下参数
[EnableCors(origins: "your_domain", headers: "*", methods: "GET")]
重新部署您的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