.netcore 杂项
https 跳转到 http 时的 Referer 丢失
获取服务自身监听的地址:
/var server = serviceProvider.GetRequiredService<IServer>();
var providerHost = hostServer.Features.Get<IServerAddressesFeature>();
需要 IServiceProvider 注入即可。
2、
RouteUrl 生成Url的bug
services.AddMvc(options => { options.EnableEndpointRouting = false; //BUG ,如果不设置成false则使用Url.RouteUrl 无法生成路由 }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
已经确实是bug,将在3.0时修复该bug。具体:https://github.com/aspnet/AspNetCore/issues/4549
3、
NETCORE 中的API 路由设置
[HttpGet("get/{id}")]
[HttpGet("get")]
public IActionResult Get(int id)
可以兼容 /get/6 和 get/?id=6 和 get?id=6 三种方式,但是后两种无法将id值传给action的id参数,使用Request.Query["id"]获取即可
但是:当单独使用 [HttpGet("get")] 标注Action 时 上文中的?id=6 的值可以传如到Action 参数中。
不知道这是不是BUG
4、
当controller 使用 [ApiController] 时候可以使用 [HttpPost("name")] 设置路由名称,此时[ActionName("name")]无效
当controller 不适用 ApiController 特性的时候,需要使用 [HttpPost] 特性和 [ActionName("name")] 来设置路由名称
core5 中,当 Controller 上使用 [Route("[controller]")] 路由特性时候,Action上也必须使用 [Route("[action]")] 属性才能访问到
mmp
5、使用枚举获取Header
HeaderNames.ContentType 如此这般
6、预定义的Http状态码有两个地方可用
Microsoft.AspNetCore.Http.StatusCodes
System.Net.HttpStatusCode(enum)

浙公网安备 33010602011771号