任务30:RoutingMiddleware介绍以及MVC引入mvc
前面讲到app.Map的方式,它也能够实现路由app
当咱们的url是task的时候,就会执行里面的context的输出内容async
app.Map("/task", taskApp=>{ taskApp.Run(async context=>{ await context.Response.WriteAsync("this is a task...."); }); });
taskapp,这里的applicationbuilder不是同一个,它是属于另一套环境ui
通常不是比较复杂应用咱们不会使用,而且它和路由的实现机制也不同this
介绍通常使用路由的方式url
首先把middleware加进来。这里加的是依赖注入的配置spa
public void ConfigureServices(IServiceCollection services) { services.AddRouting(); }
前提条件必须引入命名空间3d
using Microsoft.AspNetCore.Routing;
app.UseRouter 它给咱们一个RouterBuilder。code
builder里面mapGet和mapPost等方法orm
app.UseRouter(builder=>builder.MapGet("action",async context=>{ await context.Response.WriteAsync("this is a action"); }));
执行dotnet run
http://localhost:5000/action
另一种方式,直接传一个router给它
clear清空窗口
执行:dotnet run
一样的运行结果
http://localhost:5000/action
route的流程
core的源码
MVC中添加route
mvcrouterHandler