Data Pasing Techniques in ASP .Net Core

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:01:00

ViewData

ViewData is a dictionary-like container in ASP.NET Core used to pass data from a controller action to a view. It is typically used for one-way communication from the controller to the view.

Example:

ViewData["Message"] = "Hello from ViewData!";

ViewBag

ViewBag is a dynamic property in ASP.NET Core that is used to pass data from a controller to a view. It's similar to ViewData but provides a more concise way to access data in views.

Example:

ViewBag.Message = "Hello from ViewBag!";

TempData

TempData is used to store data temporarily across multiple requests. It is often used for scenarios like redirecting to another action and carrying data along with the request.

Example:

TempData["Message"] = "Hello from TempData!";

Session

Session in ASP.NET Core allows you to store user-specific data that persists across multiple requests. It provides a way to maintain user state.

Example:

HttpContext.Session.SetString("Username", "JohnDoe");

Cookies

Cookies are small pieces of data stored on the user's browser. They can be used to store information like user preferences or authentication tokens.

Example:

Response.Cookies.Append("Username", "MohanKumar", new CookieOptions { Expires = DateTime.Now.AddHours(1) });

Query String

The query string is a part of a URL that allows you to pass data to a server in key-value pairs. It's commonly used to send data to a web application via URL parameters.

Example:

public IActionResult MyAction()
{
  string paramValue = HttpContext.Request.Query["paramName"];
  // ...
}
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this