ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程,简单编程
ASP.NET Core 配置 MVC
前面几章节中,我们都是基于 ASP.NET 空项目 模板创建的 HelloWorld 上做开发
通过这个最基本的 HelloWorld 项目,我们了解了很多知识,初窥了 ASP.NET Core,并对 ASP.NET Core 的运行机制有了一个基本的了解
MVC 模式是 Web 开发中最重要的一个模式之一,通过 MVC,我们可以将控制器、模型和视图区分开来
ASP.NET Core 同样支持 MVC 模式,而且是通过中间件的形式来支持 MVC 模式的开发
MVC 中间件
一般情况下,ASP.NET Core 2.1 内置并下载了 Microsoft.AspNetCore.Mvc
程序集
所以我们并不需要使用 NuGet
来做一些额外的安装
我们只需要给我们的应用程序中注册 Microsoft.AspNetCore.Mvc
中间件即可
配置 MVC 中间件
我们需要将 ASP.NET Core MVC 所需的所有服务注册到运行时中
我们在 Startup
类中的 ConfigureServices()
方法中执行此操作
注册完毕后,我们将添加一个简单的控制器,然后使用控制器做一些简单的输出
-
我们先在跟目录下创建一个目录
Controllers
目录,用于存放所有的控制器右键点击 HelloWorld 项目,然后选择 添加 -> 新建文件夹,并把文件夹命名为
Controllers
-
添加完成后 解决方案资源管理器 中显示如下
-
右键点击
Controllers
目录,然后选择 添加 -> 新建文件 打开新建文件对话框如果你的电脑是 Windows ,则是 添加 -> 新建项
-
在新建文件对话框中,选中左边的 General,然后选中右边的 空类
如果你的电脑是 Windows ,则是先选中 ASP.NET 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 https://go.microsoft.com/fwlink/?LinkID=398940 public 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."
意思是 ASP.NET Core 没有找到必须的 Mvc 服务
ASP.NET 核心框架本身由具有非常专注的责任的不同小型组件组成
例如,有一个组件必须定位和实例化控制器,但该组件需要位于 ASP.NET Core MVC 的服务集合中才能正常运行
注册 MVC 服务
为了在 ASP.NET 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 https://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, 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 文件,重新启动应用,刷新浏览器,终于可以看到结果了
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 异常和错误处理 上一章节中,我们学习了 ASP.NET Core 中的 中间件, 知道中间件是一种装配到应用程序管道以处理请求和响应的组件 本章节我们继续来深入了解中间件,这次我们学习的将会是异常和错误处理中间件 当我们的 ASP.NET Core 应用程序出现错误时,我们有多种方式处理它们 上一章节中提到的 Microsoft.AspNetCore.Diagnostics NuGet 包提供了相关的附加的中间件,这些中间件可以帮助我们处理异常和错误 app.UseDeveloperExceptionPage 中间件 为了模拟错误,我们回到 app.Run() 方法,看看如果我们每次命中这个中间件时抛出异常,应用程序的行为会是怎么样的 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseD...
- 下一篇
基于Ocelot的gRpcHttp网关
什么是gRpcHttp网关 通俗的讲就是将gRpc提供的服务以rest api的形式提供出去,不需要再单独的写一个webapi去做这件事。 gRpcHttp网关好处 减少不必要代码,减少中间层提高通讯效率。 以前可能是这样用了gRpc网关后是这样 gRpcHttp网关提供哪些功能 可以直接加载proto生成的dll文件 可以只需要proto文件,自动解析出proto文件所提供的model和service 支持header转发 支持gRpc的四种模式 支持gRpc中间件 支持swagger(开发中) 支持proto和dll文件动态新增,不需要重启网关 Getting Started git clone https://github.com/BuiltCloud/Ocelot.GrpcHttpGateway.git cd Ocelot.GrpcHttpGateway/src cd samplesOcelotGateway and dotnet run cd samplesExamples.GrpcServer and dotnet run copy Examples.GrpcModels....
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS关闭SELinux安全模块
- CentOS8编译安装MySQL8.0.19
- Linux系统CentOS6、CentOS7手动修改IP地址
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果