Adding MVC to AspNetCore VS2017

In my earlier blog post I created a blank project and understood how the basic application structure is loading.

I created a new empty project just like my previous blog and now i am interested to build my request pipeline. Request pipelines are built in Startup.cs class, Configure() method.

But we need AspNetCore MVC Middleware before we move on.

Right click on dependencies and select “Manage NuGet Packages…” from the context menu. Select “Browse” on the Nuget Package Manager page. Now search “Microsoft.AspNetCore.Mvc”. NugetPackageAspNetCore.mvc

Install “Microsoft.AspNetCore.Mvc” into your package.

Open the “Startup.cs” class and make following changes to “ConfigureServices()” method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

AspNetCore runs the Startup.CofigureServices() method before building the request pipeline so that any services needed to create the pipeline are loaded first.

After the Startup.ConfigureServices(), AspNetCore calls the Startup.Configure() method to build the request pipeline. Update the Configure method as follows.

public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvcWithDefaultRoute();
}

app.UseMvcWithDefaultRoute() will load Mvc with a pre-configured default root. The pipeline expects a HomeController with an Index method.

AspNetCore is ready to serve an Mvc application now. Right click on your project file and create a new folder with the name “Controllers”. Right click “Controllers” folder and add a new Controller from the context menu.

AddController.PNG

There are two ways to create controllers. Either I can click “Controller…” in the context menu, or I can click “New Item…” to create a controller. Lets select “Controller…” to use scaffolding to create a controller. Visual studio will ask to add MVC dependencies.

AddMvcDependencies

Minimal Dependencies will add references to the Dlls and Middlewares that are regularly used in Mvc applications. Full Dependencies add complete project structure including Razor views, Layout files, Error Page files, Javascript libraries, configuration files and the script bundling functionalities. Currently i am selecting Minimal Dependencies to keep things simplest for this blog.

At the completion of adding dependencies a file will be opened automatically in the editor pane with the name “ScaffoldingReadMe.txt”. Dont forget to read this file there are clues to solve next possible issues that may arise unknowingly.

One of the issue that are likely to be faced is “No executables found matching command ‘dotnet-aspnet-codegenerator’“. First point in ScaffoldingReadMe.txt is the solution to this problem. I need to edit the csproj file of my project and add following XML.

<ItemGroup>
    <DotNetCliToolReference 
    Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" 
    Version="1.0.0" />
</ItemGroup>

I can now name the controller as “HomeController” and add a method “Index”.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return Ok("AspNetCore Mvc Content Loaded!");
    }
}

We are not adding any Razor view right now. Running this project with IISExpress will return the page as follows.

ResultPage

Keep checking my blog for further posts on Mvc and inner details.

Advertisements

3 thoughts on “Adding MVC to AspNetCore VS2017

Leave a Reply

Fill in your details below or click an icon to log in:

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