0

I have the following code:

 [LayoutRenderer("http-request")] public class NLogHttpRequestLayoutRenderer : AspNetRequestPostedBody { protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { base.DoAppend(builder, logEvent); var body = builder.ToString(); // after getting the type of the action's request model do serialization deserialization things ... } 

This is my nLog renderer. It will render every request body into the log system. But some bodies contain sensitive data like emails or bank cards. I want to mask the data. To do that, I need to understand what is the type of the action request. Considering, I have the following action:

 [HttpPost] [Route("api/v1/payment/pay")] [MaskRequestMethod(typeof(PaymentRequest))] public Task<BankCardActionResponse> Pay([FromBody] PaymentRequest request) { if (request == null) throw new HttpResponseException(HttpStatusCode.BadRequest); return _paymentService.PayAsync(SsoHelper.Ctn, request); } 

The question is, how can I get into the renderer the MethodInfo of the action if I have the HttpContext. Because if I get the MethodInfo I can retrieve the attribute [MaskRequestMethod(typeof(PaymentRequest))] and get the Type from the attribute. Having that Type, I can deserialize JSON body, mask it according to programmed in advance rules and serialize it again. That was the short explanation why do I need it at all.

So, the question: if I have the HttpContext can I get the MethodInfo of the action that is going to be executed?

1

1 Answer 1

1

You can use ActionFilter

using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Reflection; using System.Runtime.CompilerServices; public class CustomFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { var controllerType = context.Controller.GetType(); var actionName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName; MethodInfo method = controllerType.GetMethod(actionName); Type attType = typeof(AsyncStateMachineAttribute); // Obtain the custom attribute for the method. // The value returned contains the StateMachineType property. // Null is returned if the attribute isn't present for the method. var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(attType); //do your stuff.... } public void OnActionExecuted(ActionExecutedContext context) { // Do something after the action executes. } } 
Sign up to request clarification or add additional context in comments.

5 Comments

I think it's not gonna help me. I need to know the MethodInfo not here, but into the renderer in the moment of DoAppend method execution.
You can access HttpContext from DoAppend through HttpContextAccessor And use same code I provided above, but extracting actionName through contextAccessor
There is no the "Controller" property into the HttpContext. So, ActionExecutingContext is a bit different thing.
Try this answer instead stackoverflow.com/a/9022179/14202100
There is no guarantee that the controller will only have one method with any given name. ControllerActionDescriptor has a MethodInfo property. Why not just use that instead of trying to find it based on the name?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.