查询参数值中的ASP.NET空间

时间:2021-01-27 03:27:23

I have a ASP.NET WebAPI controller action with the following signature:

我有一个带有以下签名的ASP.NET WebAPI控制器操作:

public async Task<HttpResponseMessage> GetFoo(string something=null)

When this is called with the following query string: GetFoo?something=%20 I expect that the action is invoked with: something = " " but instead, something is set to null.

当使用以下查询字符串调用它时:GetFoo?something =%20我希望使用:something =“”调用该操作,但是将某些内容设置为null。

How can I make the controller action accept %20 as a string with a single space and pass it on to my application?

如何使控制器操作接受%20作为具有单个空格的字符串并将其传递给我的应用程序?

1 个解决方案

#1


0  

This is very surprising, but you're right. It seems that MVC parses out a single space in route values.

这是非常令人惊讶的,但你是对的。似乎MVC在路由值中解析出一个空格。

I have a solution for you, but it's more a work-around than an actual answer.

我有一个解决方案,但它更像是一个解决方案,而不是一个真正的答案。

Add this class:

添加此课程:

public sealed class AllowSingleSpaceAttribute : ActionFilterAttribute
{
    private readonly string _routeValueName;

    public AllowSingleSpaceAttribute(string valueName)
    {
        _routeValueName = valueName;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);

        if (context.ActionArguments.ContainsKey(_routeValueName))
        {
            if (context.HttpContext.Request.Query[_routeValueName] == " ")
            {
                context.ActionArguments[_routeValueName] = " ";
            }
        }
    }
}  

Then decorate your controller like this:

然后像这样装饰你的控制器:

[AllowSingleSpace("something")]
public async Task<HttpResponseMessage> GetFoo(string something=null)
{
    ...
}

You'll get what you're looking for, but it smells! I'd love to understand the reason this happens.

你会得到你想要的,但它闻起来!我很想了解这种情况发生的原因。

#1


0  

This is very surprising, but you're right. It seems that MVC parses out a single space in route values.

这是非常令人惊讶的,但你是对的。似乎MVC在路由值中解析出一个空格。

I have a solution for you, but it's more a work-around than an actual answer.

我有一个解决方案,但它更像是一个解决方案,而不是一个真正的答案。

Add this class:

添加此课程:

public sealed class AllowSingleSpaceAttribute : ActionFilterAttribute
{
    private readonly string _routeValueName;

    public AllowSingleSpaceAttribute(string valueName)
    {
        _routeValueName = valueName;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);

        if (context.ActionArguments.ContainsKey(_routeValueName))
        {
            if (context.HttpContext.Request.Query[_routeValueName] == " ")
            {
                context.ActionArguments[_routeValueName] = " ";
            }
        }
    }
}  

Then decorate your controller like this:

然后像这样装饰你的控制器:

[AllowSingleSpace("something")]
public async Task<HttpResponseMessage> GetFoo(string something=null)
{
    ...
}

You'll get what you're looking for, but it smells! I'd love to understand the reason this happens.

你会得到你想要的,但它闻起来!我很想了解这种情况发生的原因。