100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程 简单编程

ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程 简单编程

时间:2019-06-04 15:26:02

相关推荐

ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程 简单编程

原文: Core 配置 MVC - Core 基础教程 - 简单教程,简单编程

Core 配置 MVC

前面几章节中,我们都是基于空项目模板创建的 HelloWorld 上做开发

通过这个最基本的 HelloWorld 项目,我们了解了很多知识,初窥了 Core,并对 Core 的运行机制有了一个基本的了解

MVC 模式是 Web 开发中最重要的一个模式之一,通过 MVC,我们可以将控制器、模型和视图区分开来

Core 同样支持 MVC 模式,而且是通过中间件的形式来支持 MVC 模式的开发

MVC 中间件

一般情况下, Core 2.1 内置并下载了Microsoft.AspNetCore.Mvc程序集

所以我们并不需要使用NuGet来做一些额外的安装

我们只需要给我们的应用程序中注册Microsoft.AspNetCore.Mvc中间件即可

配置 MVC 中间件

我们需要将 Core MVC 所需的所有服务注册到运行时中

我们在Startup类中的ConfigureServices()方法中执行此操作

注册完毕后,我们将添加一个简单的控制器,然后使用控制器做一些简单的输出

我们先在跟目录下创建一个目录Controllers目录,用于存放所有的控制器

右键点击HelloWorld项目,然后选择添加 -> 新建文件夹,并把文件夹命名为Controllers

添加完成后解决方案资源管理器中显示如下

右键点击Controllers目录,然后选择添加 -> 新建文件打开新建文件对话框

如果你的电脑是 Windows ,则是添加 -> 新建项

在新建文件对话框中,选中左边的General,然后选中右边的空类

如果你的电脑是 Windows ,则是先选中Core下的代码, 然后选中

在名称中输入HomeController,然后点击右下角的新建按钮,创建一个HomeController.cs文件

如果你的电脑是 Windows ,则是点击右下角的新建按钮

添加完成后解决方案资源管理器中显示如下

同时可以看到HomeController.cs中的内容如下

using System;namespace HelloWorld.Controllers{public class HomeController{public HomeController(){}}}

接下来我们将设置HomeController类为我们的默认控制器,也就是访问/时默认使用HomeController来处理

修改HomeController.cs文件,为类HomeController类添加一个Index()方法

public string Index(){ return "你好,世界! 此消息来自 HomeController..."; }

文件全部内容如下

using System;namespace HelloWorld.Controllers{public class HomeController{public HomeController(){}public string Index(){ return "你好,世界! 此消息来自 HomeController..."; }}}

保存 **HomeController.cs文件,重新启动应用并刷新浏览器,显示的仍然是index.html` 中的内容

现在,我们删除wwwroot目录下的index.html文件

右键点击index.html文件,然后选择删除,在弹出的对话框中点击删除按钮

然后我们回到Startup.cs文件中,在Configure()方法中的app.UseFileServer();语句后面添加一条语句app.UseMvcWithDefaultRoute();

Startup.cs文件全部代码如下

using System;using System.IO;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Configuration;namespace HelloWorld{public class Startup{public Startup() { var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).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 /fwlink/?LinkID=398940public void ConfigureServices(IServiceCollection services){}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseFileServer();app.UseMvcWithDefaultRoute();/*app.Run(async (context) =>{var msg = Configuration["message"];await context.Response.WriteAsync(msg);});*/}}}

保存Startup.cs文件,重新启动应用,会发现启动失败,出错如下

System.InvalidOperationException: "Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' in the application startup code."

意思是 Core 没有找到必须的 Mvc 服务

核心框架本身由具有非常专注的责任的不同小型组件组成

例如,有一个组件必须定位和实例化控制器,但该组件需要位于 Core MVC 的服务集合中才能正常运行

注册 MVC 服务

为了在 Core 中使用 MVC 模式,我们必须在Startup类中的ConfigureServices方法中添加AddMvc服务

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

添加成功后,完整的Startup.cs文件如下

using System;using System.IO; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace HelloWorld { public class Startup { public Startup() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .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 /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, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseFileServer(); app.UseMvcWithDefaultRoute(); /* app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); */ } } }

保存Startup.cs文件,重新启动应用,刷新浏览器,终于可以看到结果了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。