What part of this Razor code updates the Request object?

Posted on 2011-10-16
Last Modified: 2012-05-12
In the attached demo, I see the Request["CompanyName"]; is updated since when it's a post back, the Request object has it.

Which line of code does that? And where is the controller specified?

Razor demo:

if (IsPost)
string companyname = Request["CompanyName"]; 
string contactname = Request["ContactName"]; 
<p>You entered: <br /> 
Company Name: @companyname <br /> 
Contact Name: @contactname </p> 
<form method="post" action=""> 
<legend>Add Customer</legend> 
<label for="CompanyName">Company Name:</label> 
<input type="text" name="CompanyName" value="" /> 
<label for="ContactName">Contact Name:</label> 
<input type="text" name="ContactName" value="" /> 
<input type="submit" value="Submit" class="submit" /> 

Open in new window

Question by:newbieweb
    LVL 3

    Accepted Solution

    Request is a property of the HttpContext object which is generated by ASP.NET for each request to the server. It contains a lot of information about the current request such as browser-information, form values, querystring variables and so on.

    When the code: string companyname = Request["CompanyName"];  is evaluated, the Request object looks in the Form collection and the Querystring collection for matching keys and returns the value.

    In this context, it could also have been written: string companyname = Request.Form["CompanyName"];

    Note that none of these values come from the controller, but from the ASP.NET framework itself.


    When you post a form to a ASP.NET page the ASP.NET framework generates a HttpRequest object.
    In this HttpRequest object you will find information about the request made, such as Form values, browser-information and so on. (see for full specification)
    By default a razor page will have a reference to an instance of the current HttpRequest object in a property called Request.
    By using Request["CompanyName"] you can return the value of the form-element named "CompanyName" from the posting form.


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now