[.NET/.NET Core].NET Core 应用程序中如何判断当前请求HttpContext.Request是GET还是POST?
3.61K 次浏览
4 个回答
-
在ASP.NET Core 3.1中,你还可以通过注入
IHttpContextAccessor
的服务来获取当前请求的谓词,如下:private readonly IHttpContextAccessor _httpContextAccessor; public MyPage(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; }
获取谓词方式:
var verb = _httpContextAccessor.HttpContext.Request.Method;
-
在.NET Core 3中,还可以使用
HttpMethods.Is{Verb}
方法来检测/判断当前请求的谓词,如:using Microsoft.AspNetCore.Http HttpMethods.IsPost(context.Request.Method); HttpMethods.IsPut(context.Request.Method); HttpMethods.IsDelete(context.Request.Method); HttpMethods.IsPatch(context.Request.Method); HttpMethods.IsGet(context.Request.Method);
HttpMethods
的Is{Verb}
方法有: