What the format and contentType should be?

2019-07-26 09:30发布

I'm creating an Asp.net core web api to accept the multiple part http post. Here is the raw http post, There are three text strings and one uploaded binary file (can be any type of file).

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
.....

I created the class ClosingDocInputFormatter : InputFormatter, now I need to added it the following code in 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();
}

Questions.

  1. Do I need to define the OutputFormatter since I only need to do the post part?
  2. The method o.FormatterMappings.SetMediaTypeMappingForFormat need two string parameters. What the values they should be?

0条回答
登录 后发表回答