0

I have an async task action controller called Upload, which allows a user to upload a PDF file. The PDF file is processed and the information is stored in a db. After the controller processes the PDF I need it to redirect to a confirmation View. Currently after the Upload controller finishes, I get no server e.g. 404 error etc, it simply redirects to the same Upload View, instead of Confirmation View

Can anyone tell me why? I'm pretty sure it has something to do with async tasks, maybe async tasks need to be redirected in a different way?

My Upload & Confirmation Actions in CompletedCamps Controller

        public ActionResult Upload(int? id)
        {
            CompletedCamp completedCamp = db.CompletedCamps.Find(id);
            return View(completedCamp);
        }

        [HttpPost]
        public async Task<ActionResult> Upload(HttpPostedFileBase file, int? id)
        {
            CompletedCamp completedCamp = db.CompletedCamps.Find(id);

            string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
            string filepath = Server.MapPath(Path.Combine("~/Surveys/", filename));
            file.SaveAs(filepath);

            completedCamp.SurveyName = filename;
            db.SaveChanges();

            await AzureVisionAPI.ExtractToTextFile(filepath);
            ParseSurveyText parse1 = new ParseSurveyText();
            await Task.Run(() => parse1.ParseTextFile(completedCamp.RollNumber, completedCamp.OfficialSchoolName, completedCamp.Date, filepath));

            return RedirectToAction("Confirmation", "CompletedCamps", new { id = id });
        }

        [HttpGet]
        public ActionResult Confirmation(int? id)
        {
            var camp = db.CompletedCamps.FirstOrDefault(c => c.Id == id);
            return View(camp);
        }
craig2020
  • 321
  • 1
  • 6
  • 12
  • Is your Controller name "CompletedCamps"? – Soheila Tarighi Apr 01 '20 at 15:40
  • It sure is: "public class CompletedCampsController : Controller". I get no server errors.. no 404 nothing. It just returns the same Upload view – craig2020 Apr 01 '20 at 15:43
  • If you want redirect to Confirmation Controller you should change" CompletedCamps "to "confirmation" – Soheila Tarighi Apr 01 '20 at 15:48
  • `return RedirectToAction("Confirmation", "Confirmation", new { id = id });` please test this part instead of your code. – Soheila Tarighi Apr 01 '20 at 15:49
  • are you posting the PDF via ajax? if so, check this answer: https://stackoverflow.com/a/20011363/9533368 – egnomerator Apr 01 '20 at 16:01
  • no ajax being used. I just tried this Json(Url.Action) method just to make sure it didn't solve my problem, still the same outcome – craig2020 Apr 01 '20 at 16:07
  • Double check if you are using `Post` and not a get request, see if it actually does what you think it does. See if pdf get saved correctly. – Mat J Apr 01 '20 at 19:16
  • @MatJ Everything inside the method works fine. The Surveys get uploaded and the data from the surveys gets entered into the database. At this time the page should be redirected to the Confirmation View, instead it looks like it directs to the Upload View again – craig2020 Apr 01 '20 at 19:21
  • Like I said, Can you check the network tab of your browser and see what is the response received, Is it a `302` or a `200`? Remember to check the `Preserve logs` checkbox while you are at it. – Mat J Apr 02 '20 at 05:03
  • Have you checked your RouteConfig just in case the problem is in there? – Dave Barnett Apr 02 '20 at 16:22
  • 1
    If you comment out the contents of your post Upload method bar the redirect is the behaviour the same? – Dave Barnett Apr 02 '20 at 16:24
  • Hi @DaveBarnett, if i comment out the contents of the post Upload method then the redirect works correctly. So I think I was right in saying that something in the async code is causing the problem? Is it strange that this problem doesn't occur when the app is deployed? – craig2020 Apr 02 '20 at 16:45
  • @MatJ I can't see anything obviously incorrect in the network tab – craig2020 Apr 02 '20 at 16:46
  • what if you set a break point in the post upload method and step through it. Does it reach the Redirect code without problem? – Dave Barnett Apr 02 '20 at 16:49
  • Is it a particular await that's causing it? Maybe this time comment out half of it and see if it works then. If possible try to narrow down which bit is responsible. I tried this with Task.Delay and it worked so I think there is more to it than it just being async. – Dave Barnett Apr 02 '20 at 16:51
  • What version of the .net framework are you targetting? – Dave Barnett Apr 02 '20 at 17:00
  • There is a lot going on within these 2 methods e.g. 2 API calls with a lot of data returned and some pixel manipulation of Bitmap objects, all of which depend on the last part to finish successfully. So unfortunately I can't test parts of them on their own. It is targeting .NET Framework 4.7.2. Out of curiosity, where did you put a task delay for it to work? – craig2020 Apr 02 '20 at 17:06

1 Answers1

0

If you want redirect to Confirmation Controller and Confirmation Action ,please test this code, instead of

return RedirectToAction("Confirmation", "CompletedCamps", new { id = id });

Use this code

return RedirectToAction("Confirmation", "Confirmation", new { id = id });
Soheila Tarighi
  • 487
  • 4
  • 15