how to post plain text to ASP.NET Web API endpoint?

前端 未结 6 2191
时光取名叫无心
时光取名叫无心 2020-12-04 17:34

I have an ASP.NET Web API endpoint with controller action defined as follows :

[HttpPost]
public HttpResponseMessage Post([FromBody] object text)

6条回答
  •  执笔经年
    2020-12-04 18:09

    Very late to this party with a grossly simplified solution. I had success with this code in the controller method:

           public HttpResponseMessage FileHandler()
           {
            HttpResponseMessage response = new HttpResponseMessage();
    
            using (var reader = new StreamReader(System.Web.HttpContext.Current.Request.GetBufferedInputStream()))
            {
                string plainText = reader.ReadToEnd();
            } .....}
    

    And on the client side, these are the Ajax options I used:

    var ajaxOptions = {
    url: 'api/fileupload/' + "?" + $.param({ "key": metaKey2, "File-Operation": "Remove", "removalFilePath": $removalFilePath, "Audit-Model": model, "Parent-Id": $parentId, "Audit-Id": $auditId }),
    type: 'POST', processData: false, contentType: false, data: "BOB"
    };

提交回复
热议问题