webapi - 捕获请求的自定义值提供程序 header
webapi - Custom value Provider to capture request header
在 ASP.NET Web-API 中,我试图借助本文
中提到的自定义值提供程序技术来读取 Http 请求 header 信息
https://www.c-sharpcorner.com/article/fetch-header-information-using-customvalueprovider-in-asp-ne/
现在这项技术工作正常,它为我提供了所需 header 键的值。但是我想修改 API 控制器操作方法签名以获得 header 的整个字典 object。
我的 API 控制器方法如下所示:-
[HttpPost]
[Route("user-documents")]
public async Task<IHttpActionResult> GetUserDocuments([ValueProvider(typeof(CustomHeaderProviderFactory))] string Client, SearchCriteria incomingObj)
{ .... Logic ...... }
这里是 CustomHeaderProviderFactory 的 类。
public class CustomHeaderProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(HttpActionContext actionContext)
{
return new CustomHeaderValueProvider(actionContext);
}
}
public class CustomHeaderValueProvider : IValueProvider
{
public Dictionary<string, string> objCollection;
public CustomHeaderValueProvider(HttpActionContext context)
{
objCollection = new Dictionary<string, string>();
foreach (var item in context.Request.Headers)
{
objCollection.Add(item.Key, string.Join(string.Empty, item.Value));
}
}
public bool ContainsPrefix(string prefix)
{
return objCollection.Keys.Contains(prefix);
}
public ValueProviderResult GetValue(string key)
{
if (key == null)
throw new Exception("NullReferenceException");
if (objCollection.TryGetValue(key, out string resultValue))
{
return new ValueProviderResult(resultValue, resultValue, System.Globalization.CultureInfo.InvariantCulture);
}
return null;
}
}
我想更改 API 方法的签名,以便我可以访问 API 的 body 中的完整请求 header object =] 方法。如果我可以在 API 方法中访问完整的 objCollection,那就太好了。因为我也会在方法内部使用其他 header 值。
请建议如何更改此代码。
创建自定义属性并在 Request.Properties[] 中设置键。您将能够从您的控制器操作中访问它
public class MyNewAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
actionContext.Request.Properties[<yourkey] = <your value>;
}
}
但是,为什么在控制器操作中需要此信息?如果它是您请求中需要的信息,为什么不将其包含在调用的有效负载中而不是 header?
在 ASP.NET Web-API 中,我试图借助本文
中提到的自定义值提供程序技术来读取 Http 请求 header 信息https://www.c-sharpcorner.com/article/fetch-header-information-using-customvalueprovider-in-asp-ne/
现在这项技术工作正常,它为我提供了所需 header 键的值。但是我想修改 API 控制器操作方法签名以获得 header 的整个字典 object。
我的 API 控制器方法如下所示:-
[HttpPost]
[Route("user-documents")]
public async Task<IHttpActionResult> GetUserDocuments([ValueProvider(typeof(CustomHeaderProviderFactory))] string Client, SearchCriteria incomingObj)
{ .... Logic ...... }
这里是 CustomHeaderProviderFactory 的 类。
public class CustomHeaderProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(HttpActionContext actionContext)
{
return new CustomHeaderValueProvider(actionContext);
}
}
public class CustomHeaderValueProvider : IValueProvider
{
public Dictionary<string, string> objCollection;
public CustomHeaderValueProvider(HttpActionContext context)
{
objCollection = new Dictionary<string, string>();
foreach (var item in context.Request.Headers)
{
objCollection.Add(item.Key, string.Join(string.Empty, item.Value));
}
}
public bool ContainsPrefix(string prefix)
{
return objCollection.Keys.Contains(prefix);
}
public ValueProviderResult GetValue(string key)
{
if (key == null)
throw new Exception("NullReferenceException");
if (objCollection.TryGetValue(key, out string resultValue))
{
return new ValueProviderResult(resultValue, resultValue, System.Globalization.CultureInfo.InvariantCulture);
}
return null;
}
}
我想更改 API 方法的签名,以便我可以访问 API 的 body 中的完整请求 header object =] 方法。如果我可以在 API 方法中访问完整的 objCollection,那就太好了。因为我也会在方法内部使用其他 header 值。
请建议如何更改此代码。
创建自定义属性并在 Request.Properties[] 中设置键。您将能够从您的控制器操作中访问它
public class MyNewAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
actionContext.Request.Properties[<yourkey] = <your value>;
}
}
但是,为什么在控制器操作中需要此信息?如果它是您请求中需要的信息,为什么不将其包含在调用的有效负载中而不是 header?