Encryption
Encryption

Reputation: 1897

C# WebRequest 404 Error on POST

I'm attempting to use some WebAPI I created to upload some files. I have 3 methods on the controller that are used in the process. Two of the methods work fine, but the third method that actually processes the files returns 404 Error Not Found every time I make a web request to it.

Controller Code:

 [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 Code Snippet:

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

And the JSON:

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

Every time I try the request I get a 404 Error Not Found. I would expect it to at least hit the method on the controller but even in POSTMAN I get the error:

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

What am I missing here? Does it need something else in the Header?

Upvotes: 0

Views: 1170

Answers (1)

Bartosz Czerwonka
Bartosz Czerwonka

Reputation: 1651

Change your code to:

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

Upvotes: 2

Related Questions