Content-Disposition当我通过Angular服务访问它时,我无法获得特定的header().启用CORS并将Angular HTTPClient设置为检索所有标头.
Startup.cs
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
ConfigureOAuth(app, config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
Run Code Online (Sandbox Code Playgroud)
fileController.cs
[Route("file/{idFile}")]
public HttpResponseMessage GetFile(string idFile)
{
string file;
byte[] file;
document = fileService.GetFile(idDFile, out fileName);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(file)
};
result.Content.Headers.ContentDisposition =
new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = nomeFile
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}
Run Code Online (Sandbox Code Playgroud)
fileService.ts
getFile(fileId: number): Observable<Response> {
const url = `${urlBackEnd}/file/${fileId}`;
return this.http.get(url, { observe: 'response', responseType: 'blob' }) …Run Code Online (Sandbox Code Playgroud) 问题: Swagger 在我的 .NET WebAPI 项目中运行良好,但没有列出 API 端点。
这是我从:http://localhost:62536/swagger/docs/v1
{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"Backend.WebApi"
},
"host":"localhost:62536",
"schemes":[
"http"
],
"paths":{
},
"definitions":{
}
}
Run Code Online (Sandbox Code Playgroud)
招摇配置.cs
using System.Web.Http;
using WebActivatorEx;
using Backend.WebApi;
using Swashbuckle.Application;
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace Backend.WebApi
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Backend.WebApi");
})
.EnableSwaggerUi(c =>
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个控制器示例:
测试控制器.cs
namespace Backend.WebApi.Controllers
{
[Authorize]
[RoutePrefix("api/test")]
public class TestController : ApiController
{
private …Run Code Online (Sandbox Code Playgroud)