question

fxhoca avatar image
0 Votes"
fxhoca asked fxhoca answered

Redirect not working for .net core 6 after creating file

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




dotnet-aspnet-core-general
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What does "close the code block mean" mean? Is the code you've shared within an action? Is there any way you can share a code sample we can run that reproduces this issue?

0 Votes 0 ·

Yes, the public void exists in a method. When you perform the post process, it becomes active and the process is successful. Then I want to redirect, it drops to get again. I can't follow step by step.

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

fxhoca avatar image
0 Votes"
fxhoca answered fxhoca commented

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();
         }
     }
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please post your response in English since this is the English forum.

0 Votes 0 ·

sorry i didn't realize

0 Votes 0 ·
fxhoca avatar image
0 Votes"
fxhoca answered

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"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.