Redirect not working for .net core 6 after creating file

Özal Taşçı 1 Reputation point
2022-05-09T12:31:26.903+00:00

Hello, in .net core 6 "System.IO.File.WriteAllText(FilePath, Text)" after this method "return RedirectToAction(nameof(Index));"
I want to use it but I don't get any error or a return. It creates the file normally, but I can't redirect.
When I close the code block, my redirect works. What would be the reason ?

var DirectoryPath = _hostingEnvironment.WebRootPath + slashed + "menu";
            var FileName = "anamenu.html";
            var Text = html;


            StringBuilder exportText = new StringBuilder(Text.ToString()); 
            if (!Directory.Exists(DirectoryPath))
            {
                Directory.CreateDirectory(DirectoryPath);
            }

            string FilePath = DirectoryPath + slashed + FileName; 
            if (!File.Exists(FilePath))
            {
                File.WriteAllText(FilePath, Text);
            }
            else
            { 
                try
                {
                    File.WriteAllText(@FilePath, exportText.ToString());
                }
                catch (Exception ex)
                {
                   //does not enter this field
                    throw;
                }
            }

                //it also crosses this area but does not steer
                return RedirectToAction(nameof(Index));
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,080 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 45,996 Reputation points
    2022-05-09T14:27:14.57+00:00

    RedirectToAction, like all the other methods on Controller, is not a terminating call. It simply creates a result that ultimately returns a redirect request to the browser to the given action. Hence you would almost always have it as the expression of a return statement. The return statement terminates the current function. So I would expect this code to save to the given file and then send the redirect back to the browser. The browser gets the redirect response and then sends another GET request to the server for the given URL.

    Is this not the behavior you're seeing? If not then describe in more detail exactly what is happening.

    0 comments No comments

  2. Özal Taşçı 1 Reputation point
    2022-05-09T17:11:09.567+00:00

    Actually, I don't have much to elaborate on. Let me describe it this way. It specifies sorting as html in my category. I then want to redirect to a previous listing.

        [HttpPost]
        public ActionResult Edit(AdminModuleDO adminModulesDo)
        {
            Result<AdminModuleDO> result = _service.Update(adminModulesDo);
            if (result.IsSuccess)
            {
                Functions functions = new Functions();
                functions.menuYaz(_hostingEnvironment, _categoryBL);  //this method covers the above code block up to the redirect field
    
                ViewData["message"] = "Güncelleme işlemi başarılı"; 
                return RedirectToAction(nameof(Index));
            }
            else
            {
                ViewData["message"] = result.Message;
                return View();
            }
        }
    

  3. Özal Taşçı 1 Reputation point
    2022-05-10T07:14:54.323+00:00

    I am revising, the result of the problem is related to the extension of the file. In other words, we cannot redirect after the creation of a .html file. If we create a file with a .txt extension, it allows it to redirect.

    This is also a problem, what should I do?

    ***** [SOLVED]: Extension must be ".cshtml"

    0 comments No comments