Retrieve Data from the Request Object in ASP.NET MVC

In ASP.NET MVC, the Request property defined within the controller can be used to retrieve the data about the request. They can be accessed easily in the controller.

Some of the useful properties within the Request objects includes

  1. Request.QueryString – This is a NameValueCollection which can be used to access variable sent with this request using GET method.
  2. Request.Form – This is a NameValueCollection will can be used access variables sent using POST variables.
  3. Request.Cookies – This is the cookie sent with this request.
Retrieve Data from the Request Object in ASP.NET MVC

Retrieve Data from the Request Object in ASP.NET MVC

Below is a sample code snippet demonstrating how to retrieve data from the request object in ASP.NET MVC.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace UrlsAndRoutes.Controllers

{

public class CustomerController : Controller

{

public ActionResult Index()

{

ViewBag.Controller = "Abundant Code Controller";

string QueryString = Request.QueryString["Name"];

string form1 = Request.Form["Variable1"];

string cookie1 = Request.Cookies["Cookie1"].ToString();

ViewBag.Action = "Index";

return View("ActionName");

}

}

}