2015-10-13 92 views
0

我正在嘗試使用一些WebAPI我創建上傳一些文件。我在過程中使用的控制器上有3種方法。其中兩種方法可以正常工作,但實際處理文件的第三種方法每次向Web發出Web請求時都會返回404錯誤。C#WebRequest 404錯誤在POST

控制器代碼:

[HttpPost] 
    public bool UploadAgentStatement(DateTime periodStart, DateTime periodEnd, string agentNumber, string excelFileBase64, string pdfFileBase64, string carrierCode) 
    { 
     var excelFile = Convert.FromBase64String(excelFileBase64); 
     var pdfFile = Convert.FromBase64String(pdfFileBase64); 
     var success = _apiUnitOfWork.UploadStatement(periodStart, periodEnd, agentNumber, excelFile, pdfFile, carrierCode); 

     return success; 
    } 

WebRequest的代碼段:

 request.Method = "POST"; 
     request.ContentType = !String.IsNullOrEmpty(jsonData) ? "application/json" : ""; 

     try 
     { 
      if (!String.IsNullOrEmpty(jsonData)) 
      { 
       using (var sw = new StreamWriter(request.GetRequestStream())) 
       { 
        sw.Write(jsonData); 
        sw.Flush(); 
        sw.Close(); 
       } 
      } 

      using (var resp = request.GetResponse()) 
      { 
       using (var reader = new StreamReader(resp.GetResponseStream())) 
       { 
        response = Convert.ToBoolean(reader.ReadToEnd()); 
       } 
      } 
     } 

而且JSON:

jsonData = { 
    "periodStart": "09-14-2015", 
    "periodEnd": "10-15-2015", 
    "agentNumber": "1ASDF", 
    "excelFileBase64": " ", 
    "pdfFileBase64": " ", 
    "carrierCode": "MEH" 
} 

每次我請求我得到一個404錯誤未找到。我期望它至少擊中控制器上的方法,但即使在POSTMAN中,我也會得到以下錯誤:

{ "Message": "No HTTP resource was found that matches the request URI ' http://localhost:58342/Api/StatementSvc/UploadAgentStatement '.", "MessageDetail": "No action was found on the controller 'StatementSvc' that matches the request." }

我在這裏錯過了什麼?它是否需要標題中的其他內容?

回答

2

你的代碼更改爲:

public class Dto 
    { 
     public DateTime PeriodStart { get; set; } 
     public DateTime PeriodEnd { get; set; } 
     public string AgentNumber { get; set; } 
     public string ExcelFileBase64 { get; set; } 
     public string PdfFileBase64 { get; set; } 
     public string CarrierCode { get; set; } 
    } 

    [HttpPost] 
    public bool UploadAgentStatement(Dto dto) 
    { 
     var excelFile = Convert.FromBase64String(dto.ExcelFileBase64); 
     var pdfFile = Convert.FromBase64String(dto.PdfFileBase64); 
     var success = _apiUnitOfWork.UploadStatement(dto.PeriodStart, dto.PeriodEnd, dto.AgentNumber, excelFile, pdfFile, dto.CarrierCode); 

     return success; 
    } 
+0

嗯,這的確課程的伎倆。這就是我匆忙做事的原因。 – Encryption