官术网_书友最值得收藏!

Adding MVC to ASP.NET Core

ASP.NET MVC Core does not work by default in ASP.NET Core applications. You must enable it within the code before using it. To do so, follow these steps:

  1. Open the Startup.cs file.
  2. Locate the ConfigureServices method and add the highlighted line to it:
    public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

This tells ASP.NET Core to add MVC to the available services. Pay attention — this is not enough for MVC to work, since it needs to be configured first.

  1. Locate the Configure method and add the highlighted code to it:
    public void Configure(IApplicationBuilder app, IHostingEnvironment 
env)
{
app.UseStaticFiles();

app.UseMvcWithDefaultRoute();
}

This instructs ASP.NET Core to use the service we added in the ConfigureServices method. In addition, it has a basic configuration for the MVC router. We will discuss ASP.NET Core MVC routing later in this chapter in the Routing section.

By now, you should have ASP.NET MVC configured and ready to run. To see it in action, continue to the next section, Controllers.

主站蜘蛛池模板: 红原县| 西丰县| 宁城县| 静海县| 溧水县| 方山县| 古蔺县| 叶城县| 通山县| 定远县| 漳平市| 定南县| 惠水县| 镇康县| 永昌县| 苍山县| 永安市| 碌曲县| 长顺县| 隆子县| 偏关县| 达孜县| 合阳县| 长治市| 灵台县| 屏山县| 津南区| 和硕县| 孝义市| 苗栗县| 历史| 临安市| 筠连县| 巴楚县| 九寨沟县| 加查县| 阿拉善盟| 桐庐县| 宁武县| 贡山| 深泽县|