Assignment 1 - Introduction to ASP.NET MVC using C#
Use Visual Studio 2017 (VS) for all of the course assignments.
The recommended work flow for developing ASP.NET code is to use VS 2017 to write and debug your code.
When your code is finished use VS's publish feature
to publish it on the Yorktown server.
Test your code on the server and submit the Yorktown URLs via Canvas.
Visual Studio 2017 is available in the Parks Hall computer labs (047 and 210). For your
personal PC Microsoft
offers the free Visual Studio 2017 Community Edition or
through the Microsoft Imagine programyou can get a free copy of Visual Studio Enterprise or Visual Studio for Mac.
During this course you will create only one new project in VS and add new code to it throughout
the quarter. Your hosting on Yorktown supports only one project so if you create more than one
you will run into problems on the server.
Visual Studio Tip: In this assignment you will create a folder
named Mis324Assignments. Inside this folder will be a solution file with the extension ".sln". To
open an existing project double-click on the .sln file (rather than starting Visual Studio
first). This assures that your project will open correctly.
Your U drive is a good place to store your VS solution (~250 MB). This allows you to access it from both on- and
off-campus. A thumb drive or cloud storage also works.
The assignment instructions include working samples. The code you submit
must have the same or more functionality as the working samples. Your layout may be different.
To enlarge an image in the assignment instructions right-click on the image and select "Open image in new tab."
-- Microsoft has written an excellent tutorial
Started with ASP.NET MVC 5 that describes how to create a new project in VS,
the MVC design pattern and add controllers, models, and views.
In this exercise you will create a MVC Application and display the default home page. Complete the first part of the tutorial Getting Started with the following changes.
The default view you are seeing is home/index. When no controller is specified MVC uses the home controller. When no action method is specified MVC defaults to the action method named index and its corresponding view.
- Use Visual Studio 2017 rather than 2013. Name your project Mis324Assignments rather than MvcMovie.
- Skip the final section "See this App Running on Azure."
In this exercise you will create a new controller named "helloWorld" with two action methods. The focus of this exercise is on controller action methods and routing (how a URL is mapped to a controller and action method). Typically MVC action methods return views but for the sake of simplicity this exercise return strings. Complete
the next step of the tutorial Adding a controller with the following changes:
- The tutorial provides copy-and-paste code for the controller. In this code change the namespace
from MvcMovie to the name of your project Mis324Assignments.
- Note that parameters for the Welcome action method can be specified two ways:
The first format maps the parameters directly to the parameters defined in the action method "Welcome" of the controller "HelloWorld":
public string Welcome(string name, int ID = 1)
The second format maps to the route that you added to RouteConfig. The routing assigns the parameters in the
URL to the named parameters in the action method. Both methods are acceptable in MVC. Some people prefer
the second method because of its clean look.
Complete the third part of the tutorial
Adding a View. Tips:
- The tutorial asks you to modify the Index and Welcome action methods that you created in the previous steps. Instead
add second action methods named Index2 and Welcome2 so your previous work is not overwritten. The corresponding views will also be named Index2.cshtml and Welcome2.cshtml.
- Complete! You have created your first MVC web application.
- Photos - In this exercise you will create a controller and four views that display flower photos. The exercise illustrates controller action methods and passing parameters into a controller.
- Add a new empty controller named "PhotosController" to the project that you created in the first exercise.
- Add a view for the index action method by right clicking inside the action method and selecting "Add View..." Template is Empty, "Use a layout view" is checked, click Add.
- Add the code below to the index view. HTML: "p" is paragraph, "a" is anchor tag, "ul" is unordered list, and "li" is list item. Tilde "~" is Razor syntax and points to the application's root folder.
<p>Separate view for each photo. Good for static content.
Example: <a href="~/music/about" >music/About</a></p>
<p>Use parameter to pass in ID. Good for dynamic content.
Example: <a href="~/music/details/B0000088DC">music/Details/B0000088DC</a></p>
<!-- MVC routing is configured pass to pass ID parameter in path -->
- Test the index action method and view in the browser. With the source for "index.cshtml" in the active tab press "ctrl-shift-w". This will open a new browser tab that displays the view.
- The links in index do not yet have action methods in the controller. Click on one of them and you should see a 404 message. The next steps will add action methods and views.
- Images: Static content such as images and style sheets are located in the folder "Content." Add a new folder inside the Content folder named "images." Download flowers.zip and add the flowers folder and images to your application.
- The first three links have individual action methods and views. In the controller create three new action methods named Cosmos, Dahlia, and Lotus. Format is the same as the "Index" action method.
- Create views for each of the action methods as you did for index.cshtml.
- Add the following image and anchor tag to each of the three views. Edit the image names to match the view names.
<img src="~/Content/images/flowers/Cosmos.jpg" alt="Cosmos" class="flowerPhoto" />
<p><a href="~/photos/">Flower Photos home</a></p>
- Test all three action methods/views to confirm that images display.
- Styling: Add a bit of styling to the images with the following css. Add this to the style sheet named "site.css" located in the Content folder.
box-shadow: 10px 10px 10px #c1bebe;
- Parameters: Frequently the content of a web page is determined by passing is a parameter such as custID, ISBN, productID, etc. Add the following action method that accepts flower name as a parameter and passes it to the view.
//use parameter to pass in photo id
public ActionResult photoView(string id)
ViewBag.photo = id;
The ViewBag object is used to pass simple string data to a view.
- View: Create a new view named PhotoView and replace the code with:
ViewBag.Title = ViewBag.photo;
<img src="~/Content/images/flowers/@(ViewBag.photo).jpg" alt="@(ViewBag.photo)" class="flowerPhoto" />
<p><a href="~/photos/">Flower Photos home</a></p>
The "@" symbols are Razor code, which supports C# server code in the view.
- Test: all seven links in the index view should now work.
- Publishing to the server: when you code is working correctly in VS you can upload it to the server. Steps:
- Create a network drive map to the server.
- In VS solution explorer right click on the name of your application (Mis324Assignments) and select “Publish...”
- Click “Custom” and add a profile name.
- For Publish Method select “File System.”
- Click the “...” and find your network drive map and root folder on Yorktown (your root folder has your WWU username).
The path will look similar to:
- Click “Open,” next, next, and Publish.
- The URL on the server will be similar to this:
- In the browser you should see the default page for your web site.
- Add the name of your controller and any parameters:
- Copy the Yorktown URLs from the browser and paste into Canvas.
Submission instructions: Submit assignments via
the Canvas course management system.
Submit the full URL for each exercise in the assignment, listing the URLs
in the same order that they are listed in the assignment. To minimize typos in URLs
it is strongly recommended
that you copy the URLs from the address bar of the browser
rather than trying to type them. Incorrect URLs
will not be graded and no credit will be given.
When pages are connected via navigation (as in your music store project) it is only
necessary to submit the URL of the first page.