As of AspNetCore 2.2 a MVC1004 warning appears when using a parameter in a Controller when naming the parameter a property of the complex type it is, for example [FromQuery]DateTime date, [FromQuery(Name = "date")]DateTime dts or even just DateTime date:
Warning MVC1004 Property on type 'DateTime' has the same name as parameter 'date'. This may result in incorrect model binding. Consider renaming the parameter or using a model binding attribute to override the name.
At runtime the program still binds correctly using the following web call:
https://localhost:44396/api/v1/testing/Test?date=2018-05-20T12:44:14Z
Steps to reproduce the behavior:
public void Test(DateTime date)
{
// nothing
}
The warning is not clear on what it means and what you need to do to fix it, I can't rename the parameter as it will break calling applications and I cannot use [FromQuery(Name = "date"] as it still throws the warning with that and I'm not sure what 'model binding' attributes are or how to use them correctly in this instance to fix the warning without causing breaking behaviour changes.
I was unable to find how to fix this by googling the warning and the only thing I was able to find was the source code in this repository that generates it

dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.200-preview-009648
Commit: 90d0b07dbc
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.200-preview-009648\
Host (useful for support):
Version: 2.2.0
Commit: 1249f08fed
.NET Core SDKs installed:
2.1.104 [C:\Program Files\dotnet\sdk]
2.1.200 [C:\Program Files\dotnet\sdk]
2.1.201 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.300 [C:\Program Files\dotnet\sdk]
2.1.301 [C:\Program Files\dotnet\sdk]
2.1.302 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
2.1.401 [C:\Program Files\dotnet\sdk]
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500-preview-009335 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.1.600-preview-009426 [C:\Program Files\dotnet\sdk]
2.2.101 [C:\Program Files\dotnet\sdk]
2.2.200-preview-009648 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.3-servicing-26724-03 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
@MattJeanes thanks for the report. There's a very specific class of issues that this analyzer warning is meant to address (the PR has a description of the problem - https://github.com/aspnet/Mvc/issues/7753), but it shouldn't apply to types like DateTime which do not get bound the same way as complex types. This is clearly a bug. Suppressing the analyzer warning would be the way to go here until we address the issue.
Ah alright, thanks!
I just hit this issue too, it is confusing.
Also, I know I can search, but it would be great to include an example on how to suppress the warning.
BTW i just changed the name to dateTime and the warning went away.
I am hitting this issue with a DateTime as well.
@antonioortizpola You can use the analyzer directly to apply a suppress rule for that warning. Just click the light bulb and choose âSuppress MVC1004â.
One way to suppress this would be to add #pragmas around it:
#pragma warning disable MVC1004 // Rename model bound parameter.
public async Task<IActionResult> DoSomething([FromForm(Name = "date")] DateTime createdAt)
#pragma warning restore MVC1004 // Rename model bound parameter.
{ ⊠}
@pranavkm Is there any desired way of how the fix for this should be implemented? Just a list of hard-coded types that are excluded from the check or something? I would like to help get this fixed soon.
@poke the plan was to check if the type has a type converter from string inside the analyzer. This should generally be as good as hardcoding a list. Does this help?
@pranavkm Is there a good way to check if there is a type converter registered? I wanted to do a simple TypeDescriptor.GetConverter(type) but that fails because the type is actually a ITypeSymbol. Is there even a safe way to check for type converters on custom types which could be applied at run-time there?
You could look at attributes on the type for TypeConverterAttribute - https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverterattribute?view=netframework-4.8
It's a bit of a heuristic compared to what MVC does (actually creates a converter to verify that it supports conversion from strings) - but it should be good enough
@rynowak Thatâs what I was thinking first, and it could work for custom types (although we wouldnât have a way to check whether a registered type converter actually allows conversion from string). However, DateTime doesnât actually have a type converter attribute. It seems that the converter is registered differently.
Thanks for the fix! đ
Most helpful comment
I am hitting this issue with a
DateTimeas well.@antonioortizpola You can use the analyzer directly to apply a suppress rule for that warning. Just click the light bulb and choose âSuppress MVC1004â.
One way to suppress this would be to add
#pragmas around it: