如何从 HttpRequestMessage 中获取方法名称?

Posted

技术标签:

【中文标题】如何从 HttpRequestMessage 中获取方法名称?【英文标题】:How to get method name from HttpRequestMessage? 【发布时间】:2021-11-26 08:21:04 【问题描述】:

我有如下处理程序,我想从Controller 获取方法名称。使用var methodName = request.Method; 只返回“GET”或“POST”之类的方法类型。如何获取方法名称,例如。从 'http://localhost:4200/weatherforecast/getitem/2 我想返回“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;
    

【问题讨论】:

这能回答你的问题吗? How can I get the route name in controller in ASP.NET MVC? 【参考方案1】:

HttpRequestMessage.RequestUri 返回 System.Uri 对象。 你可以使用

var Segments = filterContext.Request.RequestUri.Segments;

获取您请求的不同部分。要消除查询部分,您可以使用 HttpRequestMessage.RequestUri.Query

您需要查看与您相关的细分。在您的具体情况下,它将发送您想要的最后一段。

【讨论】:

【参考方案2】:

使用request.RequestUri.ToString()可以获取方法名。

以下代码将帮助您详细的请求和响应拦截。

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;


【讨论】:

以上是关于如何从 HttpRequestMessage 中获取方法名称?的主要内容,如果未能解决你的问题,请参考以下文章

.net 5 从 HttpRequest 中检索 HttpRequestMessage

如何发送带有 HttpRequestMessage 的证书?

如何在 .NET Core 中模拟/存根 HttpRequestMessage?

从HttpRequestMessage的Content中同步读取Stream

如何全局访问当前的 HttpRequestMessage 对象?

当原始请求有内容时如何克隆 HttpRequestMessage?