POST 上的 C# WebRequest 404 错误

C# WebRequest 404 Error on POST

我正在尝试使用我创建的一些 WebAPI 来上传一些文件。我在该过程中使用的控制器上有 3 种方法。其中两种方法工作正常,但实际处理文件的第三种方法 returns 每次我向它发出 Web 请求时都找不到 404 错误。

控制器代码:

 [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." }

我在这里错过了什么? Header 中还需要其他内容吗?

将您的代码更改为:

    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;
    }