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

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

问题描述

我已经做了一些搜索但似乎无法找到任何东西...

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

,我想映射的输入参数标头值:例如

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-验证: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]属性。
看来你应该能够通过使用模型绑定来实现这一功能,如所描述<一个href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api\">here.在模型绑定您可以访问请求和它的头文件,所以你应该能够读取头,并将其值设置为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天全站免登陆