WebApi:将参数映射到标头值 [英] WebApi: mapping parameter to header value

查看:30
本文介绍了WebApi:将参数映射到标头值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些搜索,但似乎没有找到任何东西......

I've done a few searches but haven't seem to find anything...

使用 WebApi,我想将输入参数映射到标头值:例如

Using WebApi, I would like to map an input parameter to a header value: e.g.

例如在控制器中:

public User GetUser(int id){
   ...
   return user;
}

我希望 WebApi 将 id 参数映射到标头值(例如 X-Auth:1234)...而不是 URL 参数.

I want WebApi to map the id parameter to a header value (e.g. X-Auth: 1234)... rather than an URL parameter.

支持吗?

推荐答案

我不认为这是开箱即用的支持,例如 [FromBody] 属性.似乎您应该能够通过使用模型绑定器来实现此功能,如 此处.在模型绑定器中,您可以访问请求及其标头,因此您应该能够读取标头并将其值设置为 bindingContext.Model 属性.

I don't think this is supported out of the box, like for example with the [FromBody] attribute. It seems you should be able to achieve this functionality by using Model Binders, as described here. In the model binder you have access to the request and its headers, so you should be able to read the header and set its value to the bindingContext.Model property.

进一步阅读文章,似乎自定义 HttpParameterBinding 和 ParameterBindingAttribute 是更合适的解决方案,或者至少我会这样做.您可以实现一个通用的 [FromHeader] 属性来完成这项工作.我也在解决同样的问题,所以我会在解决后发布我的解决方案.

Reading the article further, it seems a custom HttpParameterBinding and a ParameterBindingAttribute is a more appropriate solution, or at least I would go this way. You could implement a generic [FromHeader] attribute, which does the job. I am also fighting the same problem, so I will post my solution once I have it in place.

编辑 2:这是我的实现:

Edit 2: Here is my implementation:

public class FromHeaderBinding : HttpParameterBinding
{
    private string name;

    public FromHeaderBinding(HttpParameterDescriptor parameter, string headerName) 
        : base(parameter)
    {
        if (string.IsNullOrEmpty(headerName))
        {
            throw new ArgumentNullException("headerName");
        }

        this.name = headerName;
    }

    public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        IEnumerable<string> values;
        if (actionContext.Request.Headers.TryGetValues(this.name, out values))
        {
            actionContext.ActionArguments[this.Descriptor.ParameterName] = values.FirstOrDefault();
        }

        var taskSource = new TaskCompletionSource<object>();
        taskSource.SetResult(null);
        return taskSource.Task;
    }
}

public abstract class FromHeaderAttribute : ParameterBindingAttribute
{
    private string name;

    public FromHeaderAttribute(string headerName)
    {
        this.name = headerName;
    }

    public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
    {
        return new FromHeaderBinding(parameter, this.name);
    }
}

public class MyHeaderAttribute : FromHeaderAttribute
{
    public MyHeaderAttribute()
        : base("MyHeaderName")
    {
    }
}

然后你可以这样使用它:

Then you can use it like this:

[HttpGet]
public IHttpActionResult GetItem([MyHeader] string headerValue)
{
    ...
}

希望有所帮助.

这篇关于WebApi:将参数映射到标头值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆