Installing the ASP.NET Core NuGet package in your application

Follow these steps to install the NuGet package of ASP.NET MVC:

  1. Right click on the project, and select the Manage NuGet Packages option:

    Installing the ASP.NET Core NuGet package in your application

  2. Select the Include Prerelease checkbox so that the NuGet Package Manager will list out all the prerelease packages. Search for MVC and you'll get the Microsoft.AspNet.MVC package, as shown in the following result, and click on the Install button on the right-hand side:

    Installing the ASP.NET Core NuGet package in your application

  3. Review the changes:

    Installing the ASP.NET Core NuGet package in your application

  4. Once you click on Review Changes, the following dialog box will appear where you need to accept the license terms:

    Installing the ASP.NET Core NuGet package in your application

The NuGet Package Manager will download and install the ASP.NET Core and will update the project.json file and the associated references.

Now, your project.json file will have updated dependencies. The second line Microsoft.AspNet.Mvc is added:

"dependencies": { 
  "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
  "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
  "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final" 
}, 

Alternatively, you can also update the project.json with the NuGet package along with the version information. The NuGet Package Manager will automatically download and install them.

ASP.NET Core is installed in our application. Now, we need to tell our application to use ASP.NET MVC.

This needs a couple of changes to the Startup.cs file:

  1. Configure the application to add the MVC service. This can be done by adding the following line to the ConfigureServices method of the Startup class:
          services.AddMvc(); 
    
  2. Configure the routing so that our correct controllers will be picked for the incoming requests based on the URL entered. The following code snippet needs to be updated in the Configure method of the Startup.cs file:app.UseMvc(routes => { 
    app.UseMvc(routes => { 
      routes.MapRoute( 
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
    }); 
    

In the preceding statement, we are configuring the routes for our application.

In this chapter and most of the chapters in this book, we will write codes manually or choose an Empty template instead of relying on scaffolding templates. For those who are new to the term scaffolding, scaffolding is a feature that generates all the necessary boilerplate code for you for the selected item (for example, the Controller) instead of you needing to write everything. Though I agree that scaffolding templates are useful and save time in generating the boilerplate code, they hide many of the details that beginners have to understand. Once you write code manually, you'll know all the intricacies of how each of the components is contributing to the big picture. Once you are strong in the fundamentals, you can use scaffolding templates to save you time in writing the boilerplate code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset