Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
main updates pulled in, ef notes added
Browse files Browse the repository at this point in the history
  • Loading branch information
speudusa committed Nov 3, 2022
2 parents b0ce0ac + 83581bd commit dae4993
Show file tree
Hide file tree
Showing 28 changed files with 842 additions and 654 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Simple Controllers
==================

.. admonition:: Warning

Many changes have happened in the world of Visual Studio and ASP.NET since the writing of this book.
We reviewed the videos and instructions in the latest environment and framework and made notes to help you through any discrepancies.
If you get stuck following along with a video, we suggest comparing the written instructions for the problematic area.
Notes have been added to the reading that will help you locate or code what is needed to complete each chapter project.

The first of the MVC elements we'll work on implementing are the controllers. Recall that controllers
are like the traffic cops of our application. They handle the requests made from users interacting with the
application's view and update model data accordingly. Conversely, changes to model data are sent to the view
Expand Down Expand Up @@ -31,9 +38,15 @@ Controllers and Static Responses - Video

.. admonition:: Note

If you ever want to verify what code you started this video with, the `starter code <https://github.com/LaunchCodeEducation/HelloASPDotNETDemo>`__ for this video is on the ``master`` branch.
If you ever want to verify what code you started this video with, the `starter code <https://github.com/LaunchCodeEducation/HelloASPDotNETDemo>`__ for this video is on the ``main`` branch.
If you ever want to verify what code you end this video with, the `final code <https://github.com/LaunchCodeEducation/HelloASPDotNETDemo/tree/static-responses>`__ for this video is on the ``static-responses`` branch.

If you are exploring the starter code and are not able to build a website by using the "Run" button in Visual Studio,
try the following in your terminal:

#. Using the terminal, navigate into your project
#. Enter the command: ``dotnet run`` and follow the prompts in your terminal

.. admonition:: Tip

**Windows Users**: You might need to change some solution settings if you pull down this demo repository and run it on your computer.
Expand All @@ -52,7 +65,8 @@ Some tools may depend on us following the convention of the MVC design pattern,
If you want to change something about the provided structure, be sure to double check the documentation to make sure a tool does not depend on you following it!

To designate a given class as a controller within the ASP.NET framework, we extend the ``Controller`` class.
The `Controller class <https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller?view=aspnetcore-3.1>`__ provides us with the necessary members and methods to manage traffic between the three components in our MVC application.
The `Controller class <https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controller?view=aspnetcore-6.0>`_
provides us with the necessary members and methods to manage traffic between the three components in our MVC application.

.. sourcecode:: csharp

Expand All @@ -73,10 +87,22 @@ ASP.NET MVC has two different ways to map these routes: conventional routing and
**Conventional routing** establishes the routes as endpoints in one of the application's configuration files.
**Attribute routing** establishes the routes using :ref:`C# attributes <csharp-attributes>` that are placed in the controller file.

Routes in conventional routing are setup via endpoints in a configuration file called ``Startup.cs``.
Routes in conventional routing are setup via endpoints in a configuration file called ``Program.cs``.
When an HTTP request comes in, routing matches the request with an endpoint.
**Endpoints** designate the controller action that executes when the appropriate HTTP request comes into the application.

.. admonition:: Note on the Startup.cs file

ASP.NET 6.0 combines the ``Program.cs`` and ``Startup.cs`` files.
If you are following along with Sally's video, you will see the files are separated. She is using ASP.NET 3.1 in her video.
This discrepancy between ASP.NET frameworks should not affect this project.

If you started your project on your own in .NET 6.0, we recommend exploring `Sally's repo <https://github.com/LaunchCodeEducation/HelloASPDotNETDemo>`_
and see if you can identify where and how these two files were combined.

This `documentation <https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0>`_
illustrates some of the changes between .NET 5.0 and .NET 6.0.

When we created a new ASP.NET application, without adding any code, we were immediately able to run it.
This is because the MVC application we start out with already has the routing and endpoints set up.
When we open the application, we see in the ``Controllers`` directory that Microsoft has a preconfigured ``HomeController``.
Expand All @@ -95,12 +121,15 @@ If we added a new method, ``Hello()``, to the ``HomeController``, we would see t
.. admonition:: Note

Throughout this book, we will be using attribute routing to break the pattern established by conventional routing.
However, if you want to investigate further on how to use an endpoint for a rule-breaking route in conventional routing, Microsoft has a great `article <https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#special-case-for-dedicated-conventional-routes>`_ on the subject.
For example, endpoints preconfigured in ``Startup.cs`` is how we can navigate to ``localhost:5001`` and ``localhost:5001/Home/Index`` to see the same page.
However, if you want to investigate further on how to use an endpoint for a rule-breaking route in conventional routing,
Microsoft has a great `article <https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#special-case-for-dedicated-conventional-routes>`_ on the subject.
For example, endpoints preconfigured in ``Program.cs`` is how we can navigate to ``localhost:5001`` and ``localhost:5001/Home/Index`` to see the same page.

**Remember:** ``Program.cs`` contain the endpoints in an ASP.NET 6.0 application.

When adding a new controller, such as ``HelloController``, we need to make sure that routing is properly configured whether we use conventional routing or attribute routing.
We want to start by adding a new action method to ``HelloController``.
**Action methods** are the public methods in a controller class.
When adding a new controller, such as ``HelloController``, we need to make sure that routing is properly configured whether
we use conventional routing or attribute routing.
We want to start by adding a new action method to ``HelloController``. **Action methods** are the public methods in a controller class.
Action methods respond at a specific route and that response can be an update to a view, sending new data to a model, returning some simple HTML, and so on.
When we are creating a new action method, we want to think about what route the method needs to respond at, what request type the action method should respond to, and what that response entails.
Let's start by adding the following ``Index()`` method:
Expand Down Expand Up @@ -130,12 +159,10 @@ We also want to make use of attribute routing in our new ``HelloController``.
To do so, we can add attributes to our ``Index()`` method.
As you may recall from the :ref:`chapter <csharp-attributes>` on unit testing, attributes in C# lie somewhere between code and comments.
While an attribute cannot change the code inside the method or class, an attribute in attribute routing does supply critical information about routes and request types.
Attribute routing is powerful because it does not depend on any endpoint mapping info in ``Startup.cs``, can defy the pattern established by conventional routing, and specify one request type for a method to respond to.

.. admonition:: Note

ASP.NET has many different attributes that we can use in our controllers.
For a more in-depth catalog of different attributes, check out the `documentation <https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#http-verb-templates>`__.
Attribute routing is powerful because it does not depend on any endpoint mapping info in ``Program.cs``, can defy the pattern established by conventional routing, and specify one request type for a method to respond to.

ASP.NET has many different attributes that we can use in our controllers.
For a more in-depth catalog of different attributes, check out the `documentation <https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#http-verb-templates>`__.

.. index:: ! [HttpGet], ! [HttpPost], ! [Route("path")]

Expand Down
85 changes: 42 additions & 43 deletions src/chapters/aspdotnet-intro/initialize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Create a New ASP.NET Project
============================

ASP.NET is a framework in the .NET Core family that is used to build web applications.
While `ASP.NET <https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-3.1>`_ can be used to build a wide variety of web applications, we will be focusing on using it to build MVC web applications.
While `ASP.NET <https://learn.microsoft.com/en-us/aspnet/core/?view=aspnetcore-6.0>`_ can be used to build a wide variety of web applications,
we will be focusing on using it to build MVC web applications.

.. _initialize-aspdotnet-project:

Expand All @@ -11,72 +12,71 @@ Getting Started

To create a new ASP.NET MVC project, start a new project in Visual Studio.

**Windows Users:**
Windows Users
^^^^^^^^^^^^^

#. Use the **Get started** Menu to **Create a new project**.

.. figure:: figures/WindowsNewProjectPage0.png
:alt: User selects "Create a new project".

User selects "Create a new project".


#. When selecting the type of project, select **ASP.NET Core Web App (Model-View-Controller)**.

There are 2 ways to find this easily:
A. Select "Web" from the dropdown menu.
B. Use the search bar.
A. Use the search bar.
B. Select "Web" from the dropdown menu.

Once you have your project type, click *Next*.
#. Name your project ``HelloASPDotNET`` and put it in the appropriate directory for all of your classwork.
Hit Next.
#. Select the Framework. We are going to use *.NET Core 6.0*.
You do not need to adjust any other options at this point. Select Create!
#. Visual Studio creates a fully-functional web application for you.

.. figure:: figures/WindowsNewProjectPage1.png
:alt: User selects "ASP.NET Core Web App (Model-View-Controller)" by using option A's dropdown menu.

User selects "ASP.NET Core Web App (Model-View-Controller)" by using option A's dropdown menu.

#. Name your project ``HelloASPDotNET`` and put it in the appropriate directory for all of your classwork. Hit Next.

.. figure:: figures/WindowsNewProjectPage2.png
:alt: User names project and selects location for repo.
Troubleshooting
This `tutorial for Windows <https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio>`_
can help you if you are stuck.

User names project and selects location for repo.
Mac Users
^^^^^^^^^

#. Select the Framework. We are going to use *.NET Core 3.1 (Long-term support)*.
We are using this version because it is stable for creating the projects in this textbook.
You do not need to adjust any other options at this point. Select Create!
#. Open Visual Studio for Mac and select a **New**
#. The type of project is a **Web Application (Model-View-Controller)**.
In the left-side menu, select **App** from under the **Web and Console** list.
Continue on to the next screen.
#. Target Framework will be ``.NET 6.0``. You don't need to adjust any other settings at this time.
#. Name your project ``HelloASPDotNET`` and put it in the appropriate directory for all of your classwork.
Hit Next.
#. Visual Studio creates a fully-functional web application for you.

.. figure:: figures/WindowsNewProjectPage3.png
:alt: User selects ".NET Core 3.1 (Long-term support)".
Troubleshooting
Microsoft created a tutorial for creating an MVC in Visual Studio, but for the Mac version they recommend the following `guide <https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio-mac>`_.

.. admonition:: Note

The guide is working with ``.NET 5.0`` which is no longer supported. Make you that you are using ``.NET 6.0``.

**All Users:**
All Users
^^^^^^^^^

#. Visual Studio creates a fully-functional web application for you.
#. Now launch the application!

a. **Mac Users**: Click *Run*.
a. **Mac Users**: Click *Run*. It is a button that looks like a triangle pointing to the right.
b. **Windows Users**: Try clicking *IIS Express* first. If this results in
an ``HTTP Error 500.0``, use the dropdown arrow next to *IIS Express*.
Select ``HelloAspDotNet`` (or whatever you named your project) and try
launching the application again.

.. figure:: figures/iis-alternative.png
:alt: Arrow points to HelloAspDotNet in a dropdown menu.

Select ``HelloAspDotNet`` as an alternative.
#. Eventually, your browser will open and display your application.
Take note of the port number in the address bar.

#. Eventually, your browser will open and display your application. Take note of
the port number in the address bar.

.. figure:: figures/portnumber.png
:alt: Arrow points to URL indicating port number is 5001.

Taking note of the port number used by the server
Troubleshooting
Refer to the guides mentioned above

.. admonition:: Note

The home page of your application already contains a link to a tutorial from Microsoft on how to use ASP.NET MVC.
If you want extra study materials, check out that `tutorial <https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-3.1&tabs=visual-studio>`_ from the home page Microsoft designed!
If you want extra study materials, check out
that `tutorial <https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio>`_ from the home page Microsoft designed!

Explore the Code
----------------

In the ``Controllers`` directory, check out ``HomeController.cs``.
Microsoft provided the code in ``HomeController`` and that is why our application ran immediately after we created it and was full of content.
Expand All @@ -87,7 +87,7 @@ As we work on our new application, we will be adding a new controller, ``HelloCo
As you code along with the videos, you will be working on your own project.
However, should you want to review a step or double-check your code, fork LaunchCode's ``HelloASPDotNETDemo`` repository to see what the code looked like at each stage.
The repository is up on `Github <https://github.com/LaunchCodeEducation/HelloASPDotNETDemo>`_.
The ``master`` branch contains the code after creation and also shows the starting point for the next chapter.
The ``main`` branch contains the code after creation and also shows the starting point for the next chapter.

Check Your Understanding
------------------------
Expand All @@ -97,7 +97,6 @@ Check Your Understanding
True/False: You should take note of the port number the server is using to run your application.

a. True

b. False

.. ans: True! It may not run at 5001
Expand Down
8 changes: 5 additions & 3 deletions src/chapters/aspdotnet-model-validation/exercises.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ For the purposes of validation practice, make this property only able to be mark

public bool IsTrue { get { return true; } }

With ``IsTrue`` in ``AddEventViewModel``, you can use a ``[Compare]`` attribute to compare the value of the ``IsTrue`` property which is always ``true`` and the value of the property you add for registration requirements.
The `documentation <https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.compareattribute?view=netcore-3.1>`__ has more information on how the ``[Compare]`` attribute.

With ``IsTrue`` in ``AddEventViewModel``, you can use a ``[Compare]``
attribute to compare the value of the ``IsTrue`` property which is always ``true``
and the value of the property you add for registration requirements. The
`documentation <https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.compareattribute?view=net-6.0>`__
has more information on how the ``[Compare]`` attribute.
Loading

0 comments on commit dae4993

Please sign in to comment.