【发布时间】: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