小编Dyd*_*666的帖子

从针对跨源http.get请求的ASP.NET Web API 2响应中获取Angular中的特定响应头(例如,Content-Disposition)

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)

c# cors owin asp.net-web-api2 angular

11
推荐指数
1
解决办法
3430
查看次数

WebApi Swagger:API 端点空列表

问题: 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)

c# asp.net-web-api swagger owin swashbuckle

5
推荐指数
1
解决办法
1836
查看次数