Notes from building a first ASP.Net Core App (part 7)

  1. Controllers should inherit from a Controller base class. This will give you a significantly enhanced set of features related to being a controller.
  2. Go into the HomeController class and change it to inherit from Controller:
    public class HomeController : Controller
    
  3. You should have a formal way to encapsulate the decision of a controller. As a result of this, instead of returning a string from the Index() method, I decided to return a View of a model I created.
  4. At the top level I added a Models folder, and I have added a Hotels.cs class file to that. I have added the following code:
    namespace MyFirstAspNetCoreApp.wwwroot.Models
    {
        public class Hotel
        {
            public int Id { get; set; }
            public string DisplayName { get; set; }
        }
    }
    
  5. Next, I changed my Index action method to return the new View complete with model, as follows:
    public class HomeController : Controller
    {
       public IActionResult Index()
       {
          var model = new Hotel { Id = 1, DisplayName = "Hilton" };
          return View(model);
       }
    }
    
  6. Next, add a Views folder to the solution at the top level of the folder structure, and add an Index.cshtml file to that folder.
  7. As an aside, it looks like Microsoft have decided that the best place for all these MVC folders is at the top level of the project folder structure. I never would have guessed that. But if you don’t have an Index.cshtml file and you run the application, it will tell you where it is attempting to look in /Views/Home/ for the Index.cshtml file. I initially tried to put this under wwwroot, and it couldn’t find it. I put it at the top level of the project and it found it. I have since moved everything to the top level. As  a result, my wwwroot folder is now empty! My solution is now looking like this:
    my-first-asp-net-core-app-folder-structure-2
    Oh well, its all a learning experience!
  8. When moving the folders above to the root level, remember to remove the .wwwroot. from the namespaces.
  9. In the Index.cshtml folder, I have added the following:
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
    <h1>Welcome!</h1>
    <div>The hotel name is: @Model.DisplayName</div>
    </body>
    </html>
    
  10. Run the application (F5), and see the result. The Hotel name is displayed in the browser.
    my-first-asp-net-core-app-hotel-name
  11. The model is currently not strongly typed in the cshtml page. If you want to strongly type the model, you need to tell Index.cshtml what the type of the model is. Add the following to the top of the file:
    @model MyFirstAspNetCoreApp.Models.Hotel
    
  12. When you do this, any time you reference Model on the page, it should invoke intellisense and show you all the other options available on the Hotel object. Note that it is no longer in the wwwroot namespace as I’ve moved it to the root folder of the project and renamed the namespace.
  13. Next I will return a list of Hotels.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: