格式和contentType应该是什么?

时间:2016-10-04 22:45:45

标签: c# asp.net asp.net-web-api asp.net-core

我正在创建一个Asp.net核心web api来接受多个部分的http帖子。这是原始的http帖子,有三个文本字符串和一个上传的二进制文件(可以是任何类型的文件)。

POST http://localhost:5001/api/ClosingDoc HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Content-Length: 6957
Accept: application/json, text/plain, */*
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryDTtxFp41DIpArUZ0
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4

------WebKitFormBoundaryDTtxFp41DIpArUZ0
Content-Disposition: form-data; name="Id"

ANYTHING001
------WebKitFormBoundaryDTtxFp41DIpArUZ0
Content-Disposition: form-data; name="docType"

Test
------WebKitFormBoundaryDTtxFp41DIpArUZ0
Content-Disposition: form-data; name="comments"

Test
------WebKitFormBoundaryDTtxFp41DIpArUZ0
Content-Disposition: form-data; name="control.PNG"; filename="control.PNG"
Content-Type: image/png

 PNG
.....

我创建了类ClosingDocInputFormatter : InputFormatter,现在我需要在 Startup.cs 中添加以下代码。

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    var connection = Configuration.GetConnectionString("DefaultConnection");
    services.AddDbContext<Models.StaticDataContext>(options => options.UseSqlServer(connection));

    services.AddMvc(o => {
        o.InputFormatters.Add(new ClosingDocInputFormatter());
        //??? x.OutputFormatters.Add(new ClosingDocOutputFormatter()); // No need?
        o.FormatterMappings.SetMediaTypeMappingForFormat(
            "???", MediaTypeHeaderValue.Parse("????")) // What the strings should be?
    });
    services.AddCors();
}

问题。

  1. 我是否需要定义OutputFormatter,因为我只需要做post部分?
  2. 方法o.FormatterMappings.SetMediaTypeMappingForFormat需要两个字符串参数。它们的价值应该是什么?

0 个答案:

没有答案