What is my need -
I have a .net7.0 asp.net-webapp (razor). There are some parts of my cshtml that i want to customise based on per request which will be determined by c# corresponding PageModel class code.
What i did -
1 - using class static variables.
Result - blunder. got to know they are same for all users so it would have broken site functionality.
2 - using cookies
Result - cookies can only be appended to response and read on request, so data can't be transferred within one request but it is good for transferring data across requests.
3 - Viewdata["name"]
Result - found good for my need. the data is available only for that request.
What i want to know -
1 - Does the data in viewdata get destroyed after request ends or it piles up in server's memory.
2 - Is it safe to use viewdata in this way. Is there anything i should keep in mind while using it so it can be secure, doesn't load the server and provides needed functionality.
CodePudding user response:
You haven't actually given any details of what your need is, but in answer to the main question, ViewData is not stored across requests. It is a property of the PageModel, which is instantiated with each request and destroyed once the request has been processed and the response generated.
ViewData is used for passing data to views/pages. It is not strongly typed and can be error prone. For that reason, it is recommended to use public properties of the PageModel for passing data to views/pages. ViewData's primary use case is for passing data to weakly-typed Layout pages.