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.
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
- If you need to install visual studio download it at https://www.microsoft.com/net/download/ (If you'r using Windows you'll want to check "ASP.NET" and ".NET Core cross-platform development" on the workloads screen during installation.)
- Open the .sln file in visual studio
- 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 GradeBookTests 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 at this point, this is by design. As you progress through the projects more and more tests will pass. All tests should pass upon completion of the project.)
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 GradeBook project folder and type
dotnet run - To run the tests go into the GradeBookTests project folder and type
dotnet test
- 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
Note: this isn't the only way to accomplish this, however; this is what the project's tests are expecting. Implimenting 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.csfile add support for the MVC middleware and configure it to have a default route.- In the
ConfigureServicesmethod callAddMvconservicesto add support for MVC middleware. - In the
Configuremethod remove theapp.Runentirely and replace it with a call toUseMvcWithDefaultRouteonapp.
- In the
- In the
Startup.csfile add support for developer exception pages and user friendly error pages.- In the
Configuremethod beforeUseMvcWithDefaultRouteadd a condition that checks ifenvis set to "Development" usingIsDevelopement.- If Development it should call
UseDeveloperExceptionPageonappto get better detailed error pages. - Otherwise it should call
UseExceptionHandleronappand provide it the string "/Home/Error" to provide a generic "An Error Has Occurred" page. (Note : the Error page doesn't exist yet, we'll make it soon)
- 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 the "WishList/Views/Home" folder. (you will need to make some of these folders)
- The "Index" View should contain an
h1tag welcoming the user. (if your IDE creates a starting template for the view, remove the generated content, do this in any tasks require )
- Create a Generic Error View
- Create a new view "Error" in the "WishList/Views/Shared" folder. (you will need to make some of these folders)
- This view should contain a
ptag saying "An error has occurred. Please try again."
- This view should contain a
- Create a new view "Error" in the "WishList/Views/Shared" folder. (you will need to make some of these folders)
- Create a Generic Welcome View
- Create the
HomeController- Create a new Controller "HomeController" inside the "Controllers" folder (you might need to create this folder)
- This should inherrit the
Controllerclass (you will need to add a using directive for theMicrosoft.AspNetCore.Mvcnamespace)
- This should inherrit the
- Create a new Action
Indexin 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
Errorin 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 "HomeController" inside the "Controllers" folder (you might need to create this folder)
- Create Home Views
- Note: The application is now viewable in your browser!
- Create Item Model With EntityFramework Support
- Add
EntityFrameworksupport- Create a class
ApplicationDbContextthat inherits theDbContextclass in the "WishList/Data" folder. (you will need to make some of these folders) (Note :DbContextexists in theMicrosoft.EntityFrameworkCorenamespace) - Add a Constructor that accepts a parameter of type
DbContextOptions optionsand Invokes the base constructor as well (you can do this by adding: base(options)after the method signature)
- Create a class
- In the
Startupclass'sConfigureServicesmethod addEntityFrameworksupport.- Call
AddDbContext<ApplicationDbContext>onserviceswith the argumentoptions => options.UseInMemoryDatabase("WishList")to pointEntityFrameworkto the application'sDbContext. (Note : You will need to add a using statement forWishList.Data)
- Call
- Create the
Itemmodel.- Create a new class
Itemin the "WishList/Models" folder (You might need to create this folder)- This class should contain a public property
Idof typeint. - This class should contain a public property
Descriptionof typestring. - The
Descriptionproperty should have attributes ofRequiredandStringLength(50). (Note : You'll need to add a using statement forSystem.ComponentModel.DataAnnotations.)
- This class should contain a public property
- In the
ApplicationDbContextclass add new public propertyItemsof typeDbSet<Item>. (Note : You'll need to add a using statement forWishList.Models.)
- Create a new class
- Add
- Create "Item" Views
- Add support for Tag Helpers and Layout
- Create a New View "_ViewImports" in the "WishList/Views" folder.
- This view should contain
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.
- This view should contain
- Create a New View "_ViewStart" in the "WishList/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 "_ViewImports" in the "WishList/Views" folder.
- Create a "Create" View
- Create a new view "Create" in the "WishList/Views/Item" folder.
- This view should use a model of
Item. (You'll need to use the fullWishList.Models.Itemnot justItem) - This view should contain an
h3tag saying "Add item to wishlist". - This view should have a
formtag containing the attributeasp-actionset to "create". - Inside the
formtag create aninputtag with the attributeasp-forset to "Description". - Inside the
formtag create aspantag with the attributeasp-validation-forset to "Descrption". - Inside the
formtag create anbuttontag with the attributetypeset to "submit" and text "Add Item".
- This view should use a model of
- Create a new view "Create" in the "WishList/Views/Item" folder.
- Create the Item's "Index" View
- Create a new View "Index" in the "WishList/Views/Item" folder (You will need to make some of these folders)
- This view should use a model of
List<Item>. (You'll need to use the fullWishList.Models.Itemnot justItem) - This view should have an
h1tag containing "Wishlist". - After the
h1tag add anatag with an attributeasp-actionwith a value ofcreatewith the text "Add item". - Create a
ultag thatultag should contain a razor foreach loop that will iterate through eachitemof typeIteminModel - Each iteration should contain an
litag that provides theItem'sDescriptionproperty followed by anatag. - The
atag should have the attributesasp-actionset to "delete" andasp-route-idset to theItem'sIdproperty with the text of theatag being "delete".
- This view should use a model of
- Create a new View "Index" in the "WishList/Views/Item" folder (You will need to make some of these folders)
- In Home's Index view add an
atag with attributesasp-actionset to "Index" andasp-controllerset to "Item" with text "View wishlist".
- Add support for Tag Helpers and Layout
- Create
ItemControllerand it's Actions- Create a new Controller
ItemControllerinside theControllersfolder that inherits theControllerclass fromMicrosoft.AspNetCore.Mvc- Create a
privatereadonlyfield_contextof typeApplicationDbContext. (Do not instantiate it at this time) (Note : You will need to add a using statement toWishList.Data.) - Create a new constructor that accepts a parameter of type
ApplicationDbContext- This constructor should set
_contextto the providedApplicationDbContextparameter.
- This constructor should set
- Create a new Action
Indexin 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
Createin 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
Createin 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
Itemto_context.Items(Note : Don't forget toSaveChanges!) - This action should
RedirectToActionto theIndexaction.
- This action should have an attribute
- Create a new Action
Deletein theItemController.- This action should accept an
intparameter named "Id"; - This action should return a type of
IActionResult. - This action should get the
Itemto be deleted from_context.Itemsthen use_context.Items - This action should remove the
Itemwith the matchingIdproperty from_context.Items. (Note : Don't forget toSaveChanges!) - This action should
RedirectToActionto theIndexaction.
- This action should accept an
- Create a
- Create a new Controller
- Add Middleware/Configuration to
You've compeleted the tasks of this project, if you want to continue working on this project there will be additional projects added to the ASP.NET Core path that continue where this project left off implimenting authentication, more advanced view and models, as well as providing and consuming data as a webservice.
Otherwise now is a good time to continue on the ASP.NET Core path to expand your understanding of the ASP.NET Core framework or take a look at the Microsoft Azure for Developers path as Azure is a common choice for hosting, scaling, and expanding the functionality of ASP.NET Core applications.