ASP.NET Core - 设置MVC

在本章中,我们将在FirstAppDemo应用程序中设置MVC框架.我们将继续在ASP.NET Core之上构建一个Web应用程序,更具体地说,是ASP.NET Core MVC框架.我们可以在技术上仅使用中间件构建整个应用程序,但ASP.NET Core MVC为我们提供了可用于轻松创建HTML页面和基于HTTP的API的功能.

设置MVC在我们的空项目框架中,按照以下步骤&减去;

  • 安装 Microsoft.AspNet.Mvc package,它允许我们访问框架提供的程序集和类.

  • 安装软件包后,我们需要注册所有服务ASP.NET MVC在运行时需要.我们将在 ConfigureServices 方法中执行此操作.

  • 最后,我们需要为ASP.NET MVC添加中间件以接收请求.本质上,这个中间件接受HTTP请求并尝试将该请求定向到我们将要编写的C#类.

第1步 : 让我们通过右键单击Manage NuGet Packages来转到NuGet包管理器.安装Microsoft.AspNet.Mvc包,它允许我们访问框架提供的程序集和类.

Microsoft.AspNet.MVC

第2步 : 一旦安装了Microsoft.AspNet.Mvc软件包,我们就需要注册ASP.NET Core MVC在运行时所需的所有服务.我们将使用ConfigureServices方法执行此操作.我们还将添加一个简单的控制器,我们将看到该控制器的一些输出.

让我们在这个项目中添加一个新文件夹并将其命名为控制器.在此文件夹中,我们可以在解决方案资源管理器中放置多个控制器,如下所示.

控制器

现在右键单击Controllers文件夹并选择 Add→ Class 菜单选项.

Add Class

第3步 : 在这里,我们要添加一个简单的 C#类,并调用此类 HomeController ,然后单击上面屏幕截图中的"添加"按钮.

Home Controller

这将是我们的默认页面.

第4步 : 让我们定义一个返回字符串并调用该方法索引的公共方法,如下面的程序所示.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks;  

namespace FirstAppdemo.Controllers { 
   public class HomeController { 
      public string Index() { 
         return "Hello, World! this message is from Home Controller..."; 
      } 
   } 
}


第5步 : 当您转到网站的根目录时,您希望看到控制器响应.截至目前,我们将提供index.html文件.

控制器响应

让我们进入网站的根目录并删除index.html.我们希望控制器响应而不是 index.html 文件.

步骤6 : 现在转到Startup类中的Configure方法并添加 UseMvcWithDefaultRoute 中间件.

UseMvc默认路线

第7步 : 现在刷新网站根目录下的应用程序.

刷新应用程序

您将遇到500错误.该错误表明框架无法找到所需的ASP.NET核心MVC服务.

ASP.NET核心框架本身由不同的小组件组成,这些组件具有非常集中的职责.

例如,有一个组件必须找到并实例化控制器.

该组件需要位于ASP的服务集合中. NET Core MVC正常运行.

步骤8 : 除了添加NuGet包和中间件之外,我们还需要在ConfigureServices中添加AddMvc服务.这是Startup类的完整实现.

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Hosting; 
using Microsoft.AspNet.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Configuration;  

namespace FirstAppDemo { 
   public class Startup { 
      public Startup() { 
         var builder = new ConfigurationBuilder() .AddJsonFile("AppSettings.json"); 
         Configuration = builder.Build(); 
      }  
      public IConfiguration Configuration { get; set; }
      
      // This method gets called by the runtime. 
      // Use this method to add services to the container. 
      // For more information on how to configure your application, 
      // visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
         services.AddMvc(); 
      }
      
      // This method gets called by the runtime.  
      // Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app) { 
         app.UseIISPlatformHandler();  
         
         app.UseDeveloperExceptionPage(); 
         app.UseRuntimeInfoPage();  
         
         app.UseFileServer(); 
         app.UseMvcWithDefaultRoute();  
         
         app.Run(async (context) => { 
            var msg = Configuration["message"]; 
            await context.Response.WriteAsync(msg); 
         });
      } 
      
      // Entry point for the application. 
      public static void Main(string[] args) => WebApplication.Run<Startup>(args); 
   }  
}


第9步 : 保存 Startup.cs 文件并转到浏览器并刷新它.您现在将从我们的家庭控制器收到回复.

启动. Cs HomeController