你调用的对象是空的。' System.Web.HttpRequest.UserAgent.get返回null

时间:2019-10-10 13:48:27

标签: asp.net-web-api

我正在努力通过asp.net创建一个多部分表单并将内容传递给API 这是我的代码

 [HttpPost]
         [Route("getFileObj/{path}")]


     public async Task<string> UploadFile ()
     {
         //string data = @"C:\Users\Abhishek.Kumar31\Downloads\Expense.pdf"; same path i am giving in postman

         try
         {
             var client = new HttpClient();
             var requestContent = new MultipartFormDataContent();
             var bytes = File.ReadAllBytes(path);
             var imageContent = new ByteArrayContent(bytes);
             imageContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
             requestContent.Add(imageContent);
            client.DefaultRequestHeaders.Add("Authorization", "bearer Q4L7L6OCdEe8EPnve2Uyb2fZxsnkXdzO1DkCYQnA4WiIQdPB1EvDUyMqf74wdUtazfxNE_w_vyxsHEv3qsa7UH5udxQP9oEQbV90OzRgLQJcP46dyl-YFqTlkISKeddmjBEno5QVF0KgMjCY4A3n4jPCuGUnFxUfcXBEkBFLTapmsNm3CVT3EKwi_6w-8d-8rVs7yiRlIOlnMa9om5Vm1ni5CcAu0K64vGeHx9v7P3p6mgkFf6nD8YLEMNd7VeXNlQ8dd66RBQ8jL18y8lvvv3JwUB2vFeVM_MaOZ7BfEZm_4rfD1_CVos3mfCqcqmiplLjsHOKrzWydNWKJMrno6AOe-3vq1VNC1HVA3yMswcIjkKPaW8PLPIqJruXJSZtbv3vcnBs5CsxQHk");

             var response =  client.PostAsync("http://localhost:31002/api/imm/application/CollateDoc/22a91a31-e984-e811-972c-94659c63e114/abc/1/A4", requestContent).Result;
             var responseString = await response.Content.ReadAsStringAsync();
         }
         catch (Exception e){
         }

这是我的api代码

[Route("CollateDoc/{applicationID}/{pdfFileName}/{pdfPassword}/{pageSize}")]


            public bool CollateDocumentAndSaveToNAS(string applicationID, string pdfFileName, string pdfPassword, string pageSize)
            {

      Log4Net.Info(string.Format("User {0} collate all document to a single pdf and save to the NAS", User.Identity.Name));
                var httpRequest = HttpContext.Current.Request;
                var http = httpRequest.Files;
                var path = Path.Combine(HttpContext.Current.Server.MapPath(filepath) + documentspath, applicationID.ToString());
                string prefix = DateTime.UtcNow.ToString("yyyy-MM-dd-hh-mm-ss");

                var isCollated = CollateDocInPDF.CollateAllDocumentAndConvertToPDF(httpRequest,path, prefix, pdfFileName, pdfPassword, pageSize);


                return isCollated;
            }
        }

但我收到此错误: System.NullReferenceException:“对象引用未设置为对象的实例。” System.Web.HttpRequest.UserAgent.get返回null

有人知道我在哪里做错了吗?

0 个答案:

没有答案