.netASP.Net Razor

Objects in ASP.net Razor

Page Objects:
The most basic object in ASP.NET is the page.
You can access properties of the page object directly without any qualifying object.
The following code gets the page’s file path, using the Request object of the page:

You can use properties of the Page object to get a lot of information, such as:
? Request:As you’ve already seen, this is a collection of information about the current request, including what type of browser made the request, the URL of the page, the user identity, etc.
? Response:This is a collection of information about the response (page) that will be sent to the browser when the server code has finished running. For example, you can use this property to write information into the response.

Collection Objects (Arrays and Dictionaries):
A collection is a group of objects of the same type, such as a collection of Customer objects from a database. ASP.NET contains many built-in collections, like the Request.Files collection.
You’ll often work with data in collections. Two common collection types are the array and the dictionary.
An array is useful when you want to store a collection of similar items but do not want to create a separate variable to hold each item:

Team Members:

With arrays, you declare a specific data type, such as string, int, or DateTime.
To indicate that the variable can contain an array, you add brackets to the declaration (such as string[] or int[]).
You can access items in an array using their position (index) or by using the foreach statement.
Array indexes are zero-based — that is, the first item is at position 0, the second item is at position 1, and so on.

The number of names in the teamMembers array: @teamMembers.Length
Robert is now in position: @Array.IndexOf(teamMembers, “Robert”)
The array item at position 2 (zero-based) is @teamMembers[2]

Current order of team members in the list

Reversed order of team members in the list

You can determine the number of items in an array by getting its Length property. To get the position of a specific item in the array (to search the array), use the Array.IndexOf method. You can also do things like reverse the contents of an array (the Array.Reverse method) or sort the contents (the Array.Sort method).

A dictionary is a collection of key/value pairs, where you provide the key (or name) to set or retrieve the corresponding value:

To create a dictionary, you use the new keyword to indicate that you are creating a new dictionary object.
You can assign a dictionary to a variable using the var keyword.
You indicate the data types of the items in the dictionary using angle brackets ( < > ).
At the end of the declaration, you must add a pair of parentheses, because this is actually a method that creates a new dictionary

To add items to the dictionary, you can call the Add method of the dictionary variable (myScores in this case), and then specify a key and a value. Alternatively, you can use square brackets to indicate the key and do a simple assignment, as in the following example:

To get a value from the dictionary, you specify the key in brackets:

© 2015, www.techkatak.com. All rights reserved.