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

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.

主站蜘蛛池模板: 堆龙德庆县| 建昌县| 阳新县| 正定县| 长治县| 双城市| 台东市| 都安| 甘德县| 麻江县| 红河县| 广水市| 英超| 兴仁县| 永城市| 静乐县| 金阳县| 安福县| 当雄县| 车险| 平舆县| 阿拉尔市| 平度市| 海南省| 东阳市| 海口市| 饶平县| 安丘市| 巴南区| 墨竹工卡县| 介休市| 广丰县| 内乡县| 扶余县| 彰武县| 永和县| 大化| 抚松县| 望都县| 屏山县| 凤台县|