The Localizer (IStringLocalizerFactory) in HomeController returns sometimes what is needed, and sometimes value=key. Using LocalRedirect().

Volk Volk 551 Reputation points
2022-09-05T19:19:24.83+00:00

Hi!

Switching languages works for me as it should (the code was presented here: the-localization-language-does-not-change-currentc.html). But I needed to access the current localization in HomeController. The most interesting thing is that some data from SharedResource.resx is saved in TempData["alert"] as needed, but some return value = key. It seems that LocalRedirect sometimes clears TempData during the transition, and sometimes does not have time to do this - but I could be wrong. It even happens that the AlerttifyJS message is not shown at all after LocalRedirect. In short, the LocalRedirect system is unstable.

"thanks_for_like" property exists in the SharedResource_<lan_LAN>.resx files - 100%. To check, I tried different key values.

Can you tell me what I need to do so that TempData["alert"] is always correctly passed when using LocalRedirect?

Thanks!

237857-a.png

    public class HomeController : Controller  
    {  
      
    private IStringLocalizer _localizer;  
    private ApplicationDbContext _db;  
      
    public HomeController(  
                IStringLocalizerFactory factory,  
                ApplicationDbContext db  
    )  
    {  
            
    _localizer = factory.Create("SharedResource", Assembly.GetExecutingAssembly().GetName().Name);  
      
    _db = db;  
      
    }  
      
    public IActionResult Like(string returnUrl)  
    {              
                string message = _localizer["liked_already"].Value;  
                TempData["alert"] = message;            
                return LocalRedirect(returnUrl);  
    }  
      
    }  
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-09-06T02:19:07.127+00:00

    Hi @Volk Volk ,

    First, based on your description, I create an Asp.Net 6 MVC application to Test the TempData and LocalRedirect method, everything works well.

    It seems that LocalRedirect sometimes clears TempData during the transition, and sometimes does not have time to do this

    Second, about the TempData lost issue, I suggest you can pay attention the following points:

    1. The TempData providers using either cookies or session state. You can use F12 developer tools to check whether the cookies or session is expired.
    2. TempData stores the data temporarily and automatically removes it after retrieving a value. The data persists only from one request to the next (such as transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller), unless you mark one or more keys for retention by using the Keep method. So, after redirect the returnUrl, whether it will send another request or not? If you make another redirect, try to use the Keep method.

    More detail information about TempData, see Session and state management in ASP.NET Core

    Can you tell me what I need to do so that TempData["alert"] is always correctly passed when using LocalRedirect?

    To correctly pass the data when using the LocalRedirect, you can try to store the data in the Database or Azure Key Vault.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.

  2. Volk Volk 551 Reputation points
    2022-09-10T18:59:20.133+00:00

    The problem is I needed to replace the string new CultureInfo("en-En") with new CultureInfo("en-GB"). I also renamed the SharedResource.en-En.resx file to SharedResource.en-GB.resx. Immediately everything worked as it should. :) As far as I understand en-EN does not exist in ISO Language Code Table and it's not worth using it: langcode.htm

    Thank you all! The question is closed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.