.Net Core 路由处理
路由基础知识
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
终结点
- 用于 Razor Pages 的 MapRazorPages
- 用于控制器的 MapControllers
- 用于 SignalR 的 MapHub
- 用于 gRPC 的 MapGrpcService
app.UseRouting(); app.UseAuthentication();
app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/healthz").RequireAuthorization();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
终结点元数据
- 可以通过路由感知中间件来处理元数据。
- 元数据可以是任意的 .NET 类型。
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.Use(next => context =>
{
var endpoint = context.GetEndpoint();
if (endpoint?.Metadata.GetMetadata<AuditPolicyAttribute>()?.NeedsAudit ==true)
{
Console.WriteLine("开始处理事务逻辑");
Console.WriteLine($"ACCESS TO SENSITIVE DATA AT: {DateTime.UtcNow}");
}
return next(context);
}); app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello world!");
}); // Using metadata to configure the audit policy.
endpoints.MapGet("/sensitive", async context =>
{
await context.Response.WriteAsync($"sensitive data{DateTime.UtcNow}");
})
.WithMetadata(new AuditPolicyAttribute(needsAudit: true));
});
}
} public class AuditPolicyAttribute : Attribute
{
public AuditPolicyAttribute(bool needsAudit)
{
NeedsAudit = needsAudit;
} public bool NeedsAudit { get; }
}
比较终端中间件和路由
- 这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
- 中间件通过返回而不是调用 next 来终止管道。
- 终结点始终是终端。
终端中间件允许在管道中的任意位置放置中间件:
- 终结点在 UseEndpoints 位置执行。
- 终端中间件允许任意代码确定中间件匹配的时间:
- 自定义路由匹配代码可能比较复杂,且难以正确编写。
- 路由为典型应用提供了简单的解决方案。
- 大多数应用不需要自定义路由匹配代码。
- 带有中间件的终结点接口,如 UseAuthorization 和 UseCors。
- 通过 UseAuthorization 或 UseCors 使用终端中间件需要与授权系统进行手动交互
- 这两种方法都允许终止处理管道:终端中间件允许在管道中的任意位置放置中间件:
设置传统路由
app.UseEndpoints(endpoints =>
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
});
REST Api 的属性路由
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
- Route[]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{ [Route("Index")]
public string Index(int? id)
{
return "Test";
}
}
[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
{
public string Index(int? id)
{
return "Test";
}
}
[ApiController]
public class WeatherForecastController : ControllerBase
{
[Route("[controller]/Index")]
public string Index(int? id)
{
return "Test";
}
}
- Http[Verb]
[ApiController]
public class WeatherForecastController : ControllerBase
{
[HttpGet("[controller]/Index")]
public string Index(int? id)
{
return "Test";
}
}
- Route[]和Http[Verb]混合使用
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{ [HttpGet("Index")]
public string Index(int? id)
{
return "Test";
}
}
.Net Core 路由处理的更多相关文章
- ASP .Net Core路由(Route) - 纸壳CMS的关键
关于纸壳CMS 纸壳CMS是一个开源免费的,可视化设计,在线编辑的内容管理系统.基于ASP .Net Core开发,插件式设计: GitHub:https://github.com/SeriaWei/ ...
- 构建可读性更高的 ASP.NET Core 路由
原文:构建可读性更高的 ASP.NET Core 路由 一.前言 不知你在平时上网时有没有注意到,绝大多数网站的 URL 地址都是小写的英文字母,而我们使用 .NET/.NET Core MVC 开发 ...
- ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 路由 前两章节中,我们提到 ASP.NET Core 支持 MVC 开发 ...
- ASP.NET Core路由中间件[3]: 终结点(Endpoint)
到目前为止,ASP.NET Core提供了两种不同的路由解决方案.传统的路由系统以IRouter对象为核心,我们姑且将其称为IRouter路由.本章介绍的是最早发布于ASP.NET Core 2.2中 ...
- ASP.NET Core路由中间件[2]: 路由模式
一个Web应用本质上体现为一组终结点的集合.终结点则体现为一个暴露在网络中可供外界采用HTTP协议调用的服务,路由的作用就是建立一个请求URL模式与对应终结点之间的映射关系.借助这个映射关系,客户端可 ...
- .Net core路由高级用法
先说startup中的路由 这里是我们现在用的默认路由,但是在使用当中也有麻烦.总而言之 用的不爽. 使用属性路由:RouteAttribute特性 默认的HomeController下面的Index ...
- ASP.NET Core路由中间件[1]: 终结点与URL的映射
目录 一.路由注册 二.设置内联约束 三.默认路由参数 四.特殊的路由参数 借助路由系统提供的请求URL模式与对应终结点(Endpoint)之间的映射关系,我们可以将具有相同URL模式的请求分发给应用 ...
- 理解ASP.NET Core - 路由(Routing)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 Routing Routing(路由):更准确的应该叫做Endpoint Routing,负责 ...
- Net core学习系列(六)——Net Core路由
一.概述 路由主要有两个主要功能: 1.将请求的URL与已定义的路由进行匹配,找到该URL对应的处理程序并传入该请求进行处理. 2.根据已定义的路由生成URL 这两个功能看起来这两个是相反的. A.路 ...
- core路由设置
全局路由设置 app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exi ...
随机推荐
- Captain Technology INC浅谈新能源汽车的未来
近日全世界上最大的资管公司贝莱德向位于的英国电动汽车初创公司Arrival投资1.18亿美元,且该公司已有投资者亚马逊和美国第二大汽车制造商福特汽车参投.中国最知名的电动车公司蔚来股价单日大涨22%, ...
- 【PY从0到1】 一文掌握Pandas量化进阶
# 一文掌握Pandas量化进阶 # 这节课学习Pandas更深的内容. # 导入库: import numpy as np import pandas as pd # 制作DataFrame np. ...
- CSS绘制三角形和箭头
<html> <head> <meta charset="utf-8"> <title>CSS绘制三角形和箭头</title& ...
- 整合shiro+jwt大体思路
springboot整合shiro大体上的思路: 1.自定义一个类Realm extends AuthorizingRealm{} 主要是对token授权和认证 重写2个方法 doGetAuthori ...
- 从微信小程序到鸿蒙js开发【15】——JS调用Java
鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 目录:1.新建一个Service Ability2.完善代码逻辑3.JS端远程调用4.<从微信小 ...
- SpringCloud Stream整合RabbitMQ3.5.0
前言 点击进入Spring官网文档 本文章为单体项目,将消费者和生产者写在同一个项目中,介意者不用向下看了. 本文介绍三种应用方式: 1:普通整合RabbitMQ 2:消息分区 3:按条件消费(多个消 ...
- C++Template 模版的本质
我想知道上帝的構思,其他的都祇是細節. ...
- 博客数据库要连接Elasticsearch,使用MySQL还是MongoDB更合理
若进行博客等文本类数据的读写以及专业搜索引擎的连接的解决方案对比,可以肯定的下结论:MongoDB的解决方案中要远远好于MySQL的解决方案. 一.从开发工序角度 MySQL的文章读写方式 方式一:文 ...
- 解决浏览器点击button出现边框问题
发现问题 本人不懂浏览器的HTML代码 不知道怎么在chrome浏览器的F12之后点到了哪里 点击button的时候就会出现黑色边框 解决 终于发现不是因为动了调试页面,而是动了谷歌浏览器的高级选项, ...
- #String类简述(小白理解,小白编写,欢迎大神指点,小白跪谢)
@ 目录 一.前言(可忽略) 二.String变量的认知 三.String类的构造方法 四.String类的基本方法 4.1 toString()方法 4.2 equals()方法 4.3 equal ...