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()
- {
- }
- }
- }
- using System;
接下来我们将设置
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...";
- }
- }
- }
- public string Index()
保存 **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);
- });
- */
- }
- }
- }
- using System;
保存 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 文件,重新启动应用,刷新浏览器,终于可以看到结果了
ASP.NET Core 配置 MVC - ASP.NET Core 基础教程 - 简单教程,简单编程的更多相关文章
- net core体系-web应用程序-4net core2.0大白话带你入门-4asp.net core配置项目访问地址
asp.net core配置访问地址 .net core web程序,默认使用kestrel作为web服务器. 配置Kestrel Urls有四种方式,我这里只介绍一种.其它方式可自行百度. 在Pr ...
- Asp.net Core基于MVC框架实现PostgreSQL操作
简单介绍 Asp.net Core最大的价值在于跨平台.跨平台.跨平台.重要的事情说三遍.但是目前毕竟是在开发初期,虽然推出了1.0.0 正式版,但是其实好多功能还没有完善.比方说编译时的一些文件编码 ...
- ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 配置 上一章节我们简单介绍了下 Id ...
- ASP.NET Core 配置 EF 框架服务 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 配置 EF 框架服务 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 EF 框架服务 上一章节中我们了解了 Entity ...
- ASP.NET Core 配置 Entity Framework Core - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 配置 Entity Framework Core - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 Entity Fram ...
- ASP.NET Core 项目配置 ( Startup ) - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 项目配置 ( Startup ) - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 项目配置 ( Startup ) 前面几章节 ...
- 使用EF Core+CodeFirst建立ASP.NET Core MVC项目
本篇随笔介绍如何使用.NET Core+EF Core创建Web应用程序 首先借用官网的话简单介绍一下ASP.NET Core ASP.NET Core 是一个跨平台的高性能开源框架,用于生成基于云且 ...
- 基于ASP.NET core的MVC站点开发笔记 0x01
基于ASP.NET core的MVC站点开发笔记 0x01 我的环境 OS type:mac Software:vscode Dotnet core version:2.0/3.1 dotnet sd ...
- ASP.NET Core配置Kestrel 网址Urls
ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls.让 ...
随机推荐
- 前端css常用的选择小汇
要使用css对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器.选择器就是选择器用来指定样式的作用范围. 类选择器: 类选择器在css中比较常见,首先要在普通标签中设置 ...
- php二维数组中的查找(善于利用基础函数)
php二维数组中的查找(善于利用基础函数) 一.总结 真没必要完整的写函数,善于借用 1.array_search()是在以为数组中来找,现在我们要在二维数组数组中来,肯定要借用这个 2.!==fal ...
- 用Eclipse替代Keil&IAR来开发ARM应用(升级版)
Eclipse GNU ARM Plugin 2014/7/16 作者 kiya 几个月前写了一篇<),想自己丰衣足食的参考我的上一篇文章,以及GNU ARM的官网. 用Eclipse替代Kei ...
- iOS开发NSOperation 三:操作依赖和监听以及线程间通信
一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...
- 在RedHa上安装MRTG监控网卡流量
http://os.51cto.com/art/201103/252149.htm 2011-03-30 15:05 张微波 phpchina 字号:T | T 在RedHa上安装MRTG监控网卡流量 ...
- web网站如何获取用户的地理位置
web网站如何获取用户的地理位置 一.总结 一句话总结:通过gps知道用户的经度和纬度,然后通过经度和纬度在在地图(google或者百度)上面显示位置. 1.html5如何通过gps知道用户的经度和纬 ...
- [CSS] Change the auto-placement behaviour of grid items with grid-auto-flow
We can change the automatic behaviour of what order our grid items appear. We can even re-order the ...
- Codeforces Round #313 (Div. 2) 560C Gerald's Hexagon(脑洞)
C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Delphi程序的自我修改
前言: 对于Delphi在编译时对代码所做的工作,大部分使用Object Pascal之类的高级语言的程序员并不是很熟悉.如果你对汇编程序以及EXE文件格式有一点基本认识,那么源代码里包含的注 ...
- 机器学习: Softmax Classifier (三个隐含层)
程序实现 softmax classifier, 含有三个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...