Saturday, August 6, 2022

Using TempData, Peek And Keep In ASP.NET MVC

  •  If you set value for TempData and do not read the value then the data will be available for next request.
  • If you set value for TempData and read in View then the data will be deleted or will be null.

  • If you read TempData in the first request and want to keep the value for the next request then use 'Keep' Method.
  • If you read the TempData using 'Peek' then value persists for the next request also. 

You can use Peek() when you always want to hold/prevent the value for another request. You can use Keep() when prevent/hold the value depends on additional logic.

Explain Dependency Resolution?


Dependency Resolver again has been introduced in MVC3 and it is greatly simplified the use of dependency injection in your applications. This turn to be easier and useful for decoupling the application components and making them easier to test and more configurable.

What “beforeFilter()”,”beforeRender” and “afterFilter” functions do in Controller?

 beforeFilter(): This function runs before every action in the controller. It’s the right place to check for an active session or inspect user permissions.

beforeRender(): This function is called after controller action logic, but before the view is rendered. This function is not often used but may be required if you are calling render() manually before the end of a given action.

afterFilter(): This function is called after every controller action and after rendering is done. It is the last controller method to run.