Web Api参数绑定:snake_case到camelCase [英] Web Api Parameter binding: snake_case to camelCase

查看:110
本文介绍了Web Api参数绑定:snake_case到camelCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个新的Web api项目,我的方法/参数遵循标准的c#命名格式;方法名称为PascalCase,参数为camelCase.

I'm doing some work on a new web api project, and my methods/ parameters follow the standard c# naming format; the method name is PascalCase, the parameters are camelCase.

不幸的是,我刚刚发现向客户发布的文档的所有参数均为ruby/php样式,并带有snake_case类型的参数名称.

Unfortunately I just discovered that documentation to a client has been published with all the parameters being ruby/ php style, with snake_case type parameter names.

返回对象和POST对象很容易转换;我使用了 crallen的代码的版本来替换默认的Json输入/输出,但是在GET请求中,似乎没有这么简单的答案.

The return objects and POST object were easy to convert; I used a version of crallen's code to replace the default Json input/ output, but on GET requests, there doesn't seem to be such an easy answer.

我希望保持我的命名约定不变.有没有一种方法可以告诉绑定程序针对所有请求将my_parameter自动更改为myParameter?我必须建立一个完全不同的活页夹吗?

I'd prefer to keep my naming conventions the same. Is there a way to tell the binder to automatically change my_parameter into myParameter for all requests? Do I have to build a completely different binder?

例如,如果我将其作为方法签名:

For example, if I have this as a method signature:

[Route("~/api/Widgets")]
[ResponseType(typeof(Widget))]
public async Task<HttpResponseMessage> GetWidget(int widgetId, int groupId)
{
    . . .

我希望能够在URL中使用它

I would like to be able to use this in the URL

https://myserver.com/api/Widgets?widget_id=12345&group_id=54321

我是否需要重新发明轮子才能使其正常工作?我已经看到了替换特定类型模型联编程序的示例,但在此级别上什么也没有.我最好只是在代码中更改参数名称吗?

Do I have to reinvent the wheel to get this to work? I've seen examples of replacing specific type model binders, but nothing at this level. Am I better off just changing my parameter names in code?

推荐答案

您可以通过使用自定义的 ApiControllerActionSelector 来实现此目的,该代码重写 Request.RequestUri ,然后调用基选择器.

You can achieve this by using a custom ApiControllerActionSelector that rewrites Request.RequestUri and then calls the base selector.

在这里:

首先,创建自定义选择器:

First, create the custom selector:

public class SnakeCaseActionSelector : ApiControllerActionSelector
{
    public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
    {
        var newUri = CreateNewUri(
            controllerContext.Request.RequestUri, 
            controllerContext.Request.GetQueryNameValuePairs());

        controllerContext.Request.RequestUri = newUri;

        return base.SelectAction(controllerContext);
    }

    private Uri CreateNewUri(Uri requestUri, IEnumerable<KeyValuePair<string, string>> queryPairs)
    {
        var currentQuery = requestUri.Query;
        var newQuery = ConvertQueryToCamelCase(queryPairs);
        return new Uri(requestUri.ToString().Replace(currentQuery, newQuery));
    }

    private static string ConvertQueryToCamelCase(IEnumerable<KeyValuePair<string, string>> queryPairs)
    {
        queryPairs = queryPairs
            .Select(x => new KeyValuePair<string, string>(x.Key.ToCamelCase(), x.Value));

        return "?" + queryPairs
            .Select(x => String.Format("{0}={1}", x.Key, x.Value))
            .Aggregate((x, y) => x + "&" + y);
    }
}

接下来,创建一些扩展名以将其转换为驼峰大小写并将其转换为大写单词:

Next, create some extensions to convert to camel case and to convert to capitalized words:

public static class StringExtensions
{
    public static string ToCamelCase(this string source)
    {
        var parts = source
            .Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);

        return parts
            .First().ToLower() +
            String.Join("", parts.Skip(1).Select(ToCapital));
    }

    public static string ToCapital(this string source)
    {
        return String.Format("{0}{1}", char.ToUpper(source[0]), source.Substring(1).ToLower());
    }
}

最后将操作选择器添加到 WebApiConfig :

And finally add the action selector to the WebApiConfig:

config.Services.Replace(typeof(IHttpActionSelector), new SnakeCaseActionSelector());

这篇关于Web Api参数绑定:snake_case到camelCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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