C# 全局捕获无效的HTTP请求方法
我想将我的Web API端点限制为某些HTTP方法,例如GET和POST。我搜索了互联网,发现您可以在方法上方添加C# 全局捕获无效的HTTP请求方法,c#,asp.net,asp.net-web-api,logging,httprequest,C#,Asp.net,Asp.net Web Api,Logging,Httprequest,我想将我的Web API端点限制为某些HTTP方法,例如GET和POST。我搜索了互联网,发现您可以在方法上方添加[HttpGet]或[HttpPost],如下所示: [HttpPost] public ActionResult Login(string userName, string password) { // do login stuff return View(); } { "message": "The requested resource does not
[HttpGet]
或[HttpPost]
,如下所示:
[HttpPost]
public ActionResult Login(string userName, string password) {
// do login stuff
return View();
}
{
"message": "The requested resource does not support http method 'POST'."
}
现在我想测试上面使用[HttpPost]
的示例是否真的有效,因此我使用postman向我的Web API发送HTTP请求。我填写URI并将方法设置为GET。我得到的答复如下:
[HttpPost]
public ActionResult Login(string userName, string password) {
// do login stuff
return View();
}
{
"message": "The requested resource does not support http method 'POST'."
}
我能够验证添加[HttpPost]
是否会阻止我使用HTTP GET请求
我现在想做的是,当应用程序正在等待POST时,每当用户尝试发送GET请求时,记录事件,反之亦然。我可以为每一个方法实现一些东西,但这需要很多时间,而且一旦实现了就不容易进行更改。所以我想对它进行全局过滤或者其他什么
例如:
class MethodRequestFilter : SomeGlobalMethodFilter
{
public override void Filter(SomeRequestContext requestContext)
{
if (usedRequestMethod.isNotValid == true)
{
//implementation for logging
}
}
}
当然,我还没有在.Net的库中找到这个。当用户试图发出不受支持的请求时,如何全局记录事件
您好
Damien.一种方法是使用公共基本控制器,要实现,您需要添加一个基本控制器,该控制器将继承自
ApiController
public class BaseController : ApiController
{
public override async Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
try
{
HttpResponseMessage response = await base.ExecuteAsync(controllerContext, cancellationToken);
if (!response.IsSuccessStatusCode) // or if(response.StatusCode == HttpStatusCode.BadRequest)
{
//log here
}
return response;
}
catch(Exception ex)
{
return await InternalServerError(ex).ExecuteAsync(cancellationToken);
}
}
}
所以,一旦您调用登录方法,它将首先调用BaseController方法,您将在那里得到响应
希望这有帮助 多亏了用户,我才能够通过使用实现日志记录的基本控制器来解决问题。以下是我必须采取的步骤:
BaseController
的新控制器类并继承ApiController
:ApiController
重写ExecuteAsync
方法:BaseController
public class BaseController : ApiController
{
public override async Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
try
{
HttpResponseMessage response = await base.ExecuteAsync(controllerContext, cancellationToken);
return response;
}
catch (HttpResponseException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.MethodNotAllowed)
{
//Logging implementation
}
return Request.CreateErrorResponse(ex.Response.StatusCode, ex.Message);
}
}
}
公共类BaseController:ApiController
{
公共覆盖异步任务ExecuteAsync(HttpControllerContext controllerContext,CancellationToken CancellationToken)
{
尝试
{
HttpResponseMessage response=wait base.ExecuteAsync(controllerContext,cancellationToken);
返回响应;
}
捕获(HttpResponseException ex)
{
if(ex.Response.StatusCode==HttpStatusCode.MethodNotAllowed)
{
//日志记录实现
}
返回请求.CreateErrorResponse(例如Response.StatusCode,例如Message);
}
}
}
如果有任何方法可以改进我的代码,请让我知道:)那么,捕获请求后您想做什么?只是记录?我想通过向我的REST服务器发送一个警报来记录事件。然后,你可以通过创建基本(自定义)控制器来实现它,该控制器将从api控制器继承,所有其他控制器将从基本控制器继承,一旦用户请求特定的方法,就从基本控制器调用它并检查是否有任何错误..等等,你的意思是,你不想使用[HttpGet]和[HttpPost]?@Div我只想使用HttpGet和HttpPost,但每个请求都不同。在我的登录请求示例中,我只接受HttpPost并尝试在其上使用HttpGet,这当然不应该起作用。每当我对请求应用[HttpGet]或[HttpPost]时,我都希望记录恶意用户尝试执行的所有其他请求。感谢您的建议,这对我帮助很大:)