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

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.

主站蜘蛛池模板: 阳谷县| 墨玉县| 花莲县| 洛南县| 怀远县| 邵阳市| 武功县| 新晃| 亳州市| 阳谷县| 彭州市| 天水市| 新源县| 托里县| 镇安县| 英德市| 镇雄县| 石门县| 西贡区| 水城县| 浦县| 花垣县| 大新县| 桐庐县| 滦平县| 琼中| 海宁市| 吐鲁番市| 潮安县| 新晃| 郯城县| 承德县| 中江县| 垣曲县| 静海县| 紫金县| 武清区| 邵武市| 丹凤县| 四子王旗| 江达县|