0

I have handler like below and I want to get method name from Controller. Using var methodName = request.Method; just return only type of method like 'GET' or 'POST'. How can I get method name, ex. from 'http://localhost:4200/weatherforecast/getitem/2 I want to return "getitem".

public class RequestAndResponseHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (request.Content != null)
        {
            string requestBody = await request.Content.ReadAsStringAsync();
            Trace.WriteLine(requestBody);
        }
        var response = await base.SendAsync(request, cancellationToken);

        if (response.Content != null)
        {
            var responseBody = await response.Content.ReadAsStringAsync();
            Trace.WriteLine(responseBody);
        }

        var methodName = request.Method;

        return response;
    }
}
kenzolek
  • 344
  • 6
  • 24
  • Does this answer your question? [How can I get the route name in controller in ASP.NET MVC?](https://stackoverflow.com/questions/363211/how-can-i-get-the-route-name-in-controller-in-asp-net-mvc) – Carrivas Nov 26 '21 at 08:34

2 Answers2

0

HttpRequestMessage.RequestUri returns the System.Uri object. you can use

var Segments = filterContext.Request.RequestUri.Segments;

to get different parts of your request. to eleminate query part, you cna use HttpRequestMessage.RequestUri.Query

you will need to see which segment is relevant to you. in your specific case, it will be send last segment that you want.

0

using request.RequestUri.ToString() you can get method name.

the following code will help you in detail Request and Response intercept.

public class CustomLogHandler : DelegatingHandler
{

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
    var logMetadata = await BuildRequestMetadata(request);
    var response = await base.SendAsync(request, cancellationToken);
    logMetadata = await BuildResponseMetadata(logMetadata, response);
    await SendToLog(logMetadata);
    return response;
}
private async Task<LogMetadata> BuildRequestMetadata(HttpRequestMessage request)
{
    LogMetadata log = new LogMetadata
    {
        RequestMethod = request.Method.Method,
        RequestTimestamp = DateTime.Now,
        RequestUri = request.RequestUri.ToString(),
        RequestContent = await request.Content.ReadAsStringAsync(),
    };
    return log;
}
private async Task<LogMetadata> BuildResponseMetadata(LogMetadata logMetadata, HttpResponseMessage response)
{
    logMetadata.ResponseStatusCode = response.StatusCode;
    logMetadata.ResponseTimestamp = DateTime.Now;
    logMetadata.ResponseContentType = response.Content == null ? string.Empty : response.Content.Headers.ContentType.MediaType;
    logMetadata.Response = await response.Content.ReadAsStringAsync();
    return logMetadata;
}
private async Task<bool> SendToLog(LogMetadata logMetadata)
{
    try
    {
     //write this code
    }
    catch
    {
        return false;
    }
    return true;
}
}
Sunny Jangid
  • 578
  • 4
  • 19