Visual Studio 2012
Set up the Visual Studio development environment and test the web server on your machine. More information on how you can get the most recent version of VS is found in Course Home.
Students must use Visual Studio 2013 or prior versions of Visual Studio to complete this assignment.
STEP A: Create a Model-View-Controller Application
MVC applications are not created as Visual Studio Web Sites. Instead we create a Project, similar to what we do for a desktop application. Follow the directions in this week’s lecture to create an ASP.NET MVC web application. The application should be named web460_wk7_ilab.
The name is very important because this project uses namespaces. If the namespace in your code files is not web460_wk7_ilab, you can either:
• delete the project and recreate a new with the correct name (web460_wk7_iLab), or
• adjust the code in the iLab to match the namespace of your application.
When you build and run the default web application, you should see a website similar to the image below that has a responsive web layout. As you adjust the window size, elements move to accommodate the narrower screen size, such as for a mobile device.
Because URLs are very important in determining the classes and actions called in an MVC application, you should note the URL and port number IIS Express is using to run the application. localhost refers to your own computer, and the number following is the port. In the image below, the port is 51071.
In the examples shown in the lab steps below, replace the port number (51071) with the port number on your computer.
STEP B: Adding a Controller
In this step, we will build a controller that returns HTML directly to the browser. This will help us begin with a simple example before adding more features. In the next step we will have the controller call a View( ) to create a full web page.
1. Right-click on the Controllers folder and select Add => Controller… from the menu.
2. Name the controller HelloEarthController and select the Empty MVC controller template.
3. You should see HelloEarthController.cs in the Controllers folder in theSolution Explorer pane. Double-click the file to open it and examine the code. The index action method is the default action method for each controller. Replace the HelloEarthController Index( ) action method with the following code:
4. Run the application and append HelloEarth to the base URL:
You should see the following:
5. We just called the HelloEarth Controller. The URL routing logic in ASP.NET MVC applications is a format like this to determine what code to invoke:/[Controller]/[ActionName]/[Parameters]
Add the following Welcome action method to the HelloEarth Controller:
6 Run the application and append HelloEarth/Welcome to the base URL:
You should see the following:
7 Now, let’s pass information in arguments to the Welcome method. Change the Welcome method to the following:
8 Run the application and again append HelloEarth/Welcome to the base URL:
You should see the default values for visitor and numTimes:
9 Run the application and again append query string parameters to the base URL:
MVC will bind the query string values to the action method parameters. You should see:
STEP C: Creating a View
In this step we’ll create a View that displays default information when the site visitor goes to /HelloEarth.
1. Return to the HelloEarth Controller action method and return it to the original version (don’t forget to change the return type back toActionResult):
2. To add a View for the Index method, right-click inside the Index method and select Add View…
3. In the Add View dialog, the view name should be Index and the view engine should be ASPX (C#). Also be sure the Use layout or master page is checked and Site.Master is selected as the master page.
4. You should now see a HelloEarth folder containing Index.aspx under theViews folder.
5. Double-click HelloEarth/Index.aspx to examine the code and you should see four content areas from the master page referenced. In the Content1container, enter the following (this is the page title):
Hello Earth Index
6. In the Content2 container, enter the following (this is the main content heading):
Welcome to the Earth Index!
7. In the Content3 container, enter the following:
This web application is your introduction Earth culture.
8. Run the application and append HelloEarth to the base URL:
You should see the following:
STEP D: Passing To the View From the Controller
MVC uses an object called the ViewBag to communicate between the controller and views. The ViewBag is a dynamic object that can have data fields added to it on the fly (as the program is running). We will use the ViewBag to send theWelcome View data to display on the web page.
9. Return to the HelloEarth Controller and change the Welcome action method to look like the code below (don’t forget to change the return type back toActionResult). We are setting the VisitorName and NumTimes data fields of the ViewBag object. Those data fields are not part of the ViewBag object but will be added when these statements are executed. In our example, we simply pass the parameters to the View, but we could also pass computed values or values retrieved from a Model.
10. Right-click on the Welcome action method and select Add View… just as we did for the Index action method above. The %> name should be Welcome, use the ASPX (C#) view engine and the master page Site.Master. After clicking OK, you should see the file Welcome.aspx inside the HelloEarthfolder under Views.
11. To access the ViewBag data fields in the View, we enclose it in <%: and %>as an ASPX directive. Open Welcome.aspx for editing. Inside the Content2container, place the following:
Welcome to Earth <%: ViewBag.VisitorName %>!
12. Let’s use ASPX Page directives along with the ViewBag data to repeatedly print our the visitor’s name. Add the code below to the Content3 container:
1. Run the application and again append query string parameters to the base URL:
MVC will bind the query string values to the action method parameters. TheController will then pass those to the View. You should see:
STEP E: Finalize the iLab
1. Save your work!
2. Test it! Make changes as appropriate until it works.
3. Remember to add comments for each step being performed.
4. Please zip and submit the entire web project folder to the assignment Dropbox.
* You can also use 2CO option if you want to purchase through Credit Cards/Paypal but make sure you put the correct billing information otherwise you wont be able to receive any download link.
* Your paypal has to be pre-loaded in order to complete the purchase or otherwise please discuss it with us at [email protected].
* As soon as the payment is received, download link of the solution will automatically be sent to the address used in selected payment method.
* Please check your junk mails as the download link email might go there and please be patient for the download link email. Sometimes, due to server congestion, you may receive download link with a delay.
* All the contents are compressed in one zip folder.
* In case if you get stuck at any point during the payment process, please immediately contact us at [email protected] and we will fix it with you.
* We try our best to reach back to you on immediate basis. However, please wait for atleast 8 hours for a response from our side. Afterall, we are humans.
* Comments/Feedbacks are truely welcomed and there might be some incentives for you for the next lab/quiz/assignment.
* In case of any query, please donot hesitate to contact us at [email protected].
* MOST IMPORTANT Please use the tutorials as a guide and they need NOT to be used for any submission. Just take help from the material.
******************************************** Good Luck ***************************************************
Any personal information received will only be used to fill your order. We will not sell or redistribute your information to anyone.
We will try our best to resolve the issue and if still persists we can discuss for a refund in case its required.