ASP.NET Core WishList Application
The ASP.NET Core WishList Application is designed to allow users to create their own wishlists, and other users to mark that they are buying those items in such a way the owner of the wish list isn't able to see, while other users are able to see. This application is designed using the Model View Controller design pattern.
Note: This project is the first in a series of four projects, this project will cover taking an empty ASP.NET Core web application, setting up it's middleware to support MVC and EntityFramework, then creating a simple single user wishlist application.
Setup the Application
If you want to use Visual Studio
If you want to use Visual Studio (highly recommended) follow the following steps:
- If you already have Visual Studio installed make sure you have .Net Core installed by running the "Visual Studio Installer" and making sure ".NET Core cross-platform development" is checked
- Open the .sln file in visual studio
- If you need to install visual studio download it at https://www.microsoft.com/net/download/ (If you're using Windows you'll want to check "ASP.NET" and ".NET Core cross-platform development" on the workloads screen during installation.)
- To run the application simply press the Start Debug button (green arrow) or press F5
- If you're using Visual Studio on Windows, to run tests open the Test menu, click Run, then click on Run all tests (results will show up in the Test Explorer)
- If you're using Visual Studio on macOS, to run tests, select the WishListTests Project, then go to the Run menu, then click on Run Unit Tests (results will show up in the Unit Tests panel)
(Note: All tests should fail! It's ok. This is supposed to happen. As you complete the project, more of the tests will pass. When you complete the project, all tests should pass)
If you don't plan to use Visual studio
If you would rather use something other than Visual Studio
- Install the .Net Core SDK from https://www.microsoft.com/net/download/core once that installation completes you're ready to roll!
- To run the application go into the WishList project folder and type
dotnet run
- To run the tests go into the WishListTests project folder and type
dotnet test
Features you will implement
- Setup and configure middleware to support MVC
- Create the ability to view your wishlist
- Create the ability add items to your wish list
- Create the ability to remove items from your wishlist
Tasks necessary to complete implementation:
Note: this isn't the only way to accomplish this, however; this is what the project's tests are expecting. Implementing this in a different way will likely result in being marked as incomplete / incorrect.
- Creating ASP.NET Core Application from scratch
- Add Middleware/Configuration to
Startup.cs
- In the
Startup.cs
file add support for the MVC middleware and configure it to have a default route.- In the
ConfigureServices
method callAddMvc
onservices
to add support for MVC middleware. - In the
Configure
method remove theapp.Run
entirely and add a call toUseRouting()
onapp
. - In the
Configure
method afterUseRouting
add a call toUseEndpoints()
onapp
with an argument ofendpoints => { endpoints.MapDefaultControllerRoute(); }
- In the
- In the
Startup.cs
file add support for developer exception pages and user friendly error pages.- In the
Configure
method beforeUseRouting
add a condition that checks ifenv
is set toDevelopment
usingIsDevelopement
.- If Development it should call
UseDeveloperExceptionPage
onapp
to get better detailed error pages. - Otherwise, it should call
UseExceptionHandler
onapp
and pass it the argument "/Home/Error". Next, we'll create the generic Error page provided by this method.
- If Development it should call
- In the
- In the
- Create Home Views and
HomeController
- Create Home Views
- Create a Generic Welcome View
- Create a new view
Index
in theWishList/Views/Home
folder. (you will need to make some of these folders) - The
Index
View should contain anh1
tag welcoming the user. (Note: you should remove any generated HTML from this new view and any other views you create)
- Create a new view
- Create a Generic Error View
- Create a new view
Error
in theWishList/Views/Shared
folder.- This view should contain a
p
tag saying "An error has occurred. Please try again."
- This view should contain a
- Create a new view
- Create a Generic Welcome View
- Create the
HomeController
- Create a new Controller
HomeController
inside theControllers
folder (you might need to create this folder)- This should inherit the
Controller
class (you will need to add ausing
statement for theMicrosoft.AspNetCore.Mvc
namespace)
- This should inherit the
- Create a new Action
Index
in theHomeController
- This action should have a return type of
IActionResult
. - The return statement should return the
Index
view (specify the "Index" view in your return statement).
- This action should have a return type of
- Create a new Action
Error
in theHomeController
- This action should have a return type of
IActionResult
. - The return statement should return the
Error
view (specify the "Error" view in your return statement).
- This action should have a return type of
- Create a new Controller
- Create Home Views
- Note: The application is now viewable in your browser!
- Create Item Model With EntityFramework Support
- Add
EntityFramework
support- Create a class
ApplicationDbContext
that inherits theDbContext
class in the "WishList/Data" folder. (you will need to make some of these folders) (Note :DbContext
exists in theMicrosoft.EntityFrameworkCore
namespace) - Add a Constructor that accepts a parameter of type
DbContextOptions options
and invokes the base constructor (you can do this by adding: base(options)
after the method signature)
- Create a class
- In the
Startup
class'sConfigureServices
method addEntityFramework
support.- Call
AddDbContext<ApplicationDbContext>
onservices
with the argumentoptions => options.UseInMemoryDatabase("WishList")
to pointEntityFramework
to the application'sDbContext
. (Note : You will need to add ausing
statement forWishList.Data
)
- Call
- Create the
Item
model.- Create a new class
Item
in the "WishList/Models" folder (You might need to create this folder)- This class should contain a public property
Id
of typeint
. - This class should contain a public property
Description
of typestring
. - The
Description
property should have attributes ofRequired
andMaxLength(50)
. (Note : You'll need to add ausing
statement forSystem.ComponentModel.DataAnnotations
.)
- This class should contain a public property
- In the
ApplicationDbContext
class add new public propertyItems
of typeDbSet<Item>
. (Note : You'll need to add ausing
statement forWishList.Models
.)
- Create a new class
- Add
- Create "Item" Views
- Add support for Tag Helpers and Layout
- Create a New View
_ViewImports
in theWishList/Views
folder.- This view should contain
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
.
- This view should contain
- Create a New View
_ViewStart
in theWishList/Views
folder.- This view should contain
@{ Layout = "_Layout"; }
. (Note : We've provided a very basic layout for you, this layout contains some basic CSS and jQuery.)
- This view should contain
- Create a New View
- Create a "Create" View
- Create a new view "Create" in the "WishList/Views/Item" folder. (You will need to make some of these folders)
- This view should use a model of
Item
. (You'll need to use the fullWishList.Models.Item
not justItem
) - This view should contain an
h3
tag saying "Add item to wishlist". - This view should have a
form
tag containing the attributeasp-action
set to "create". - Inside the
form
tag create aninput
tag with the attributeasp-for
set to "Description". - Inside the
form
tag create aspan
tag with the attributeasp-validation-for
set to "Description". - Inside the
form
tag create anbutton
tag with the attributetype
set to "submit" and text "Add Item".
- This view should use a model of
- Create a new view "Create" in the "WishList/Views/Item" folder. (You will need to make some of these folders)
- Create the Item's "Index" View
- Create a new View "Index" in the "WishList/Views/Item" folder
- This view should use a model of
List<Item>
. (You'll need to use the fullWishList.Models.Item
not justItem
) - This view should have an
h1
tag containing "Wishlist". - After the
h1
tag add ana
tag with an attributeasp-action
with a value ofcreate
with the text "Add item". - Create a
ul
tag thatul
tag should contain a razor foreach loop that will iterate through eachitem
of typeItem
inModel
- Each iteration should contain an
li
tag that provides theItem
'sDescription
property followed by ana
tag. - The
a
tag should have the attributesasp-action
set to "delete" andasp-route-id
set to theItem
'sId
property with the text of thea
tag being "delete".
- This view should use a model of
- Create a new View "Index" in the "WishList/Views/Item" folder
- In Home's Index view add an
a
tag with attributesasp-action
set to "Index" andasp-controller
set to "Item" with text "View wishlist".
- Add support for Tag Helpers and Layout
- Create
ItemController
and it's Actions- Create a new Controller
ItemController
inside theControllers
folder that inherits theController
class fromMicrosoft.AspNetCore.Mvc
- Create a
private
readonly
field_context
of typeApplicationDbContext
. (Do not instantiate it at this time) (Note : You will need to add ausing
statement toWishList.Data
.) - Create a new constructor that accepts a parameter of type
ApplicationDbContext
- This constructor should set
_context
to the providedApplicationDbContext
parameter.
- This constructor should set
- Create a new Action
Index
in theItemController
.- This action should have a return type of
IActionResult
. - This action should return the item's "Index" view. (Explicitly specify the view in the return statement)
- This action should provide the "Index" view with a model of type
List<Item>
that contains all items in_context.Items
.
- This action should have a return type of
- Create a new Action
Create
in theItemController
.- This action should have an attribute
HttpGet
. - This action should have a return type of
IActionResult
. - This action should return the "Create" view. (Explicitly specify the view in the return statement)
- This action should have an attribute
- Create a new Action
Create
in theItemController
.- This action should have an attribute
HttpPost
. - This action should accept a parameter of type
Item
. - This action should have a return type of
IActionResult
. - This action should add the provided
Item
to_context.Items
(Note : Don't forget toSaveChanges
!) - This action should
RedirectToAction
to theIndex
action.
- This action should have an attribute
- Create a new Action
Delete
in theItemController
.- This action should accept an
int
parameter named "Id"; - This action should return a type of
IActionResult
. - This action should get the
Item
to be deleted from_context.Items
then use_context.Items
- This action should remove the
Item
with the matchingId
property from_context.Items
. (Note : Don't forget toSaveChanges
!) - This action should
RedirectToAction
to theIndex
action.
- This action should accept an
- Create a
- Create a new Controller
- Add Middleware/Configuration to
What Now?
Now is a good time to continue on with other ASP.NET Core courses to expand your understanding of the ASP.NET Core framework. You could also take a look at the Microsoft Azure for Developers path as Azure is often used with ASP.NET Core applications.