-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable [FromQuery]
primitive says "The value 'null' is not valid"
#45500
Comments
Triage: The reason this is happening is because the query parameter You can consider leveraging a resource filter to preprocess the parameter yourself before model-binding kicks in. |
Hi @snebjorn. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Hmm perhaps I can use the resource filter. But I cannot figure out how to do it :( public class TestFilter : IResourceFilter
{
public void OnResourceExecuted(ResourceExecutedContext context) { }
public void OnResourceExecuting(ResourceExecutingContext context)
{
context.HttpContext.Request.Query["foo"] = 123; // this is readonly
}
} Also how am I supposed to fix it :) Given that the foo is already parsed as null and the Query type is |
Yes, in this case you would have to override the
Indeed, you may to implement a custom value provider. @snebjorn By the way, is there a particular reason that you want this behavior? |
Hi @snebjorn. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Besides sending Then the specific issue was a |
@snebjorn as mentioned maybe a value provider could help you. Potentially you could create a custom using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Globalization;
public class NullableQueryStringValueProvider : QueryStringValueProvider
{
public NullableQueryStringValueProvider(
BindingSource bindingSource,
IQueryCollection values,
CultureInfo? culture)
: base(bindingSource, values, culture)
{
}
public override ValueProviderResult GetValue(string key)
{
var result = base.GetValue(key);
if (result.Length > 0)
{
// Check for string `null` and recreate the ValueProviderResult with null
}
return result;
}
}
public class NullableQueryStringValueProviderFactory : IValueProviderFactory
{
/// <inheritdoc />
public Task CreateValueProviderAsync(ValueProviderFactoryContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var query = context.ActionContext.HttpContext.Request.Query;
if (query != null && query.Count > 0)
{
var valueProvider = new NullableQueryStringValueProvider(
BindingSource.Query,
query,
CultureInfo.InvariantCulture);
context.ValueProviders.Add(valueProvider);
}
return Task.CompletedTask;
}
} You will also need to add it to the ValueProviders list." builder.Services.AddControllers(options =>
{
options.ValueProviderFactories.Insert(0, new NullableQueryStringValueProviderFactory());
}); More information Model Binding in ASP.NET Core |
Another option is to leave out the query string parameter when it's unspecified. |
Hi @snebjorn. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate. See our Issue Management Policies for more information. |
Is there an existing issue for this?
Describe the bug
null
cannot be sent to a query parameter.Given this ApiController
Note: the default value is just to help see if null was passed or the parameter was omitted.
This is the result:
GET /api/foos?foo=null
-> The value 'null' is not valid.GET /api/foos?foo=
-> 42GET /api/foos?foo
-> 42GET /api/foos?
-> 42This is unfortunate as there's a difference between omitting the
foo
param and setting it tonull
Expected Behavior
GET /api/foos?foo=null
-> nullGET /api/foos?foo=
-> 42GET /api/foos?foo
-> 42GET /api/foos?
-> 42Steps To Reproduce
GET /api/foos?foo=null
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
This is somewhat related #40415 though it's for POST requests.
There's an interesting discussion about how to set query params to null. Examples:
?key
,?key=
or?key=null
.My vote is on
?key=null
.dotnet --info
The text was updated successfully, but these errors were encountered: