Issue while passing null values to nullable properties in web api call in .netcore web api project - .net-core

I am facing issue while passing null parameter values to properties of my model in HttpGet verb.
I am using .Net Core 2.1 for my web API project. Below is my action method in controller:
[HttpGet("get")]
public ActionResult GetData([FromQuery]MyTestModel model)
{
var result = new MyTestModel();
return new JsonResult(result);
}
And my MyTestModel.cs is like :
[Serializable]
public class MyTestModel
{
public MyTestModel()
{
PageNo = 1;
PageSize = 10;
}
public int ClientId { get; set; }
public int? CandidateId { get; set; }
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
public int PageNo { get; set; }
public int PageSize { get; set; }
}
When I call the API like :
api/controller/get?clientId=7583&candidateId=null&fromDate=null&toDate=null
I am getting 400 response. Below is the response message:
{"toDate":["The value 'null' is not valid for ToDate."],
"fromDate":["The value 'null' is not valid for FromDate."],
"candidateId":["The value 'null' is not valid for CandidateId."]
}
When I don't send nullable properties at all(candidateId, fromDate,toDate), this hits my action and uses default values as null.
What's the problem if I am trying to explicitly setting null values?
Do I need to set some configuration in my Startup.cs to handle null values for nullable properties?
Any help will be appreciated .
Thanks in advance.

Everything sent in the query string is just a string. So, when you do something like toDate=null, you're actually saying "set toDate to "null"", i.e. the string "null". The modelbinder attempts to convert all the strings to the actual types you're binding to, but there's no conversion available that can turn "null" into a null DateTime.
To set the value to null, you need to either pass no value toDate= or just omit the key entirely from the query string.

Related

.NET 5.0 Web API won't work with record featuring required properties

I'm using a C# 9.0 record type as a binding model for a .NET 5.0 Web API project. Some of the properties are required.
I'm using the record positional syntax, but am receiving errors.
public record Mail(
System.Guid? Id,
[property: Required]
string From,
[property: Required]
string[] Tos,
[property: Required]
string Subject,
string[]? Ccs,
string[]? Bccs,
[property: Required]
Content[] Contents,
Attachment[]? Attachments
);
This is then exposed as the binding model for my Index action:
public async Task<ActionResult> Index(Service.Models.Mail mailRequest)
{
…
}
Whenever I try to make a request, however, I receive the following error:
Record type 'Service.Models.Mail' has validation metadata defined on property 'Contents' that will be ignored. 'Contents' is a parameter in the record primary constructor and validation metadata must be associated with the constructor parameter.
I tried removing the attribute on the Contents property, but it then fails for the next (prior) property. I tried using [param: …] instead of [property: …], as well as mixing them, but keep getting the same kind of error.
I looked around the web, and haven't found any suggestion of handling annotations differently for C# 9 records. I did my best, but I'm out of ideas—outside of converting my records to POCOs.
I gave up using Positional constructor, and with the verbose full declaration of the properties, it works.
public record Mail
{
public System.Guid? Id { get; init; }
[Required]
public string From { get; init; }
[Required]
public string[] Tos { get; init; }
[Required]
public string Subject { get; init; }
public string[]? Ccs { get; init; }
public string[]? Bccs { get; init; }
[Required]
public Content[] Contents { get; init; }
public Attachment[]? Attachments { get; init; }
public Status? Status { get; init; }
public Mail(Guid? id, string #from, string[] tos, string subject, string[]? ccs, string[]? bccs, Content[] contents, Attachment[]? attachments, Status status)
{
Id = id;
From = #from;
Tos = tos;
Subject = subject;
Ccs = ccs;
Bccs = bccs;
Contents = contents;
Attachments = attachments;
Status = status;
}
}
Try using only [Required] (instead of [property: Required]), for some reason worked for me
For me it started to work by adding the [ApiController] attribute to the controller.
I found something similar on ASP.NET Core Razor pages getting:
InvalidOperationException: Record type 'WebApplication1.Pages.LoginModelNRTB+InputModel' has validation metadata defined on property 'PasswordB' that will be ignored. 'PasswordB' is a parameter in the record primary constructor and validation metadata must be associated with the constructor parameter.
from
Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.ThrowIfRecordTypeHasValidationOnProperties()
After some digging, I found: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/ModelBinding/Validation/DefaultComplexObjectValidationStrategy.cs
So maybe as you've done, the verbose declaration is the way forward.
Positional record attributes in ASP.NET Core background
How do I target attributes for a record class? more background
Using FluentValidation and keeping properties with the full declaration seems to work perfectly in my case. I highly recommend trying this highly polished alternative validation library instead of using the pretty old standard data annotations
public record LoginViewModel
{
public string Mail { get; init; }
public string Password { get; init; }
public bool IsPersistent { get; init; }
}
public class LoginValidator : AbstractValidator<LoginViewModel>
{
public LoginValidator()
{
RuleFor(l => l.Mail).NotEmpty().EmailAddress();
RuleFor(l => l.Password).NotEmpty();
}
}

ASP.net Core eager loading, let included object be null

I am getting all customers and including their linked operator.
The only catch is a customer can exist without an operator.
The problem I am having is when i try include the operator any customer that doesn't have a linked operator is not retrieved is there a way to still retrieve all my customers and if thy do not have an operator just have the operator object within the customer be null?
-get all customers method
public List<Customer> GetAllWithRelations()
{
return Context.Set<Customer>()
.Include(cp => cp.Operator).ToList();
}
-Cusomer object
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int? OperatorId { get; set; }
[ForeignKey("OperatorId")]
public virtual Operator Operator { get; set; }
}
-Operator Object
public class Operator
{
public int Id { get; set; }
public string Name { get; set; }
}
Although you did not specify a tag for this, by using the .Include I'm guessing it's a EntityFramework Core linq which is breaking.
I've came across the same case on EF whenever the relationship is not set to allow nulls. So, for instance, your mapping might be explicitly setting it to be required or somehow you're not setting it and EF defaults are stablishing a required map between Customer and Operator.
Just set it to optional wherever you're building your model mappings and you'll get the desired behavior.
See: https://learn.microsoft.com/en-us/ef/core/modeling/required-optional

How to pass model values in a parameter

I want to pass values using model as parameter.
This is basically the mvc web api app
This is my Model class
public class ConversionModel
{
public double value { get; set; }
public int qty { get; set; }
public double result { get; set; }
public string from { get; set; }
public string to { get; set; }
}
This is My controller code
[HttpGet]
[Route("api/Conversion/Currency")]
public double Currency(ConversionModel c)
{
return c.value;
}
And my url is
http://localhost:5267/api/Conversion/Currency?value=123
But is showing me an error
Object reference not set to an instance of an object.
You cannot pass an object in query string. You need to write all parameters in your Route annotation.
[Route("api/Conversion/Currency/{value}/{qty}/{result}/{from}/{to}")]
And then in your action:
public double Currency(double value, int qty, double result, string from, string to) {
var conversionModel = new ConversionModel();
conversionModel.value = value;
conversionModel.qty = qty;
conversionModel.result = result;
conversionModel.from = from;
conversionModel.to = to;
// Rest of your code.
}
You are only providing an integer value, it cannot be converted to ConversionModel.
You can either use post
[HttpPost]
public double Currency([FromBody]ConversionModel c)
which is more suitable for a complex object.
Or pass the values as separate get parameters, constructing a ConversionModel in the method body.
Or use get with [FromUri]; this still requires supplying all the individual parameter-values. (see here)
Passing a collection of individual values is a little fragile/clumsy, I would prefer to use post. Besides which, it is unlikely that you need all the values, and an instance of the class, if you will be simply returning a double value, so post is most likely to be appropriate.

POST Method fails to populate request object in ServiceStack

I've been using service stack for a while and came upon a scenario where the POST method uses the default instance of the IReturn object (with all the properties defaulting to their datatype values). The values supplied as part of the Route (/product/1234345/) are the only ones populated. I've laid out an example below:
[Route("/search/{searchMethod}/books")]
public class SearchRequest : IReturn<SearchResponse>
{
public SearchProvider searchProvider { get; set; }
public string searchTerm { get; set; }
public string categoryID { get; set; }
public long maxResults { get; set; }
//Only this property gets populated if method is post
public string searchMethod { get; set; }
}
public SearchResponse Any(SearchRequest searchRequest)
{
//This works only for non-post requests
return Put(searchRequest);
}
public SearchResponse Get(SearchRequest searchRequest)
{
//This works
return Put(searchRequest);
}
public SearchResponse Post(SearchRequest searchRequest)
{
//This does not
return Put(searchRequest);
}
public SearchResponse Put(SearchRequest searchRequest)
{
//Code for put method goes here
}
I'm then using a client to call these methods
SearchServiceClient searchClient = new SearchServiceClient(SearchServiceAPIUrl);
SearchResponse searchResponse = searchClient.Search(SearchProvider.SampleSearchProvider, searchterm, categoryID, 100,"conservative");
Any help is really appreciated
Thanks
I've always just populated my request object in the constructor and sent it to the service
searchClient.Post(new SearchRequest(SearchProvider.SampleSearchProvider,
searchterm, categoryID, 100,"conservative")):
I finally found the solution after tinkering with the DTO. It seems for post requests all DTO properties needed to have a [DataMember] attribute for serialization/deserialization and make sure that the class also has a [DataContract] attribute.

ASP.NET MVC 5 prevent value changes

When I update my website, it hints me this problem "{"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}"
The screenshot is list below, there is a value named RecordDate, it has value, but I will not change anything about that value so I didn't display it on the screen.
The problem is MVC automatically update that value for me, and the value of the date becomes 0000-00-01 i think, maybe something else, how to prevent it? just keep the origin value and update other columns.
The model class looks like this
public class ShiftRecord
{
public int ID { get; set; }
public int EmployeeID { get; set; }
[Display(Name="Company Vehicle?")]
[UIHint("YesNo")]
public bool IsCompanyVehicle { get; set; }
[Display(Name="Own Vehicle?")]
[UIHint("YesNo")]
public bool IsOwnVehicle { get; set; }
//Problem comes from this line
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString="{0:yyyy-MM-dd}")]
public DateTime RedordDate { get; set; }
[Display(Name="Day Type")]
public Nullable<DayType> DayType { get; set; }
[Display(Name="Normal Hrs")]
public Nullable<int> NormalHours { get; set; }
[Display(Name="Time and Half Hrs")]
public Nullable<int> TimeAndHalfHours { get; set; }
[Display(Name="Double Time Hrs")]
public Nullable<int> DoubleTimeHours { get; set; }
[Display(Name="Shift Hrs")]
public Nullable<int> ShiftHours { get; set; }
public string Comment { get; set; } // System manager can leave any comment here
public bool IsRead { get; set; } // has this shift record been read
public virtual Employee Employee { get; set; }
public virtual ICollection<JobRecord> JobRecords { get; set; }
}
In the controller, I didn't change anything about the model, so it looks like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,EmployeeID,IsCompanyVehicle,IsOwnVehicle,RecordDate,DayType,NormalHours,TimeAndHalfHours,DoubleTimeHours,ShiftHours,Comment,IsRead")] ShiftRecord shiftrecord)
{
if (ModelState.IsValid)
{
db.Entry(shiftrecord).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "LastName", shiftrecord.EmployeeID);
return View(shiftrecord);
}
And I didn't change Edit view as well, the only thing is I made RecordDate unchangeable, changed it from #Html.EditorFor to #Html.DisplayFor
<div class="form-group">
#Html.LabelFor(model => model.RedordDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.RedordDate)
#Html.ValidationMessageFor(model => model.RedordDate)
</div>
</div>
Your issue is .net uses a default 1/1/0001 datetime min value, and the sql minimum value is 1/1/1753, which is incompatible. If you use datetime?, it will resolve as null and work OK, or put in code to update the date to a default value before committing to the database.
Your understanding is incorrect. ASP.NET MVC did not automatically update the value for you, the problem arises because you did not post RedordDate to the controller action so RedordDate will have its default value (i.e. default(DateTime)).
DateTime is a value type in .NET such that it cannot be null and its default value is DateTime.MinValue (i.e. 01/01/0001 00:00:00).
You can solve it by making the RedordDate property Nullable by changing its type from DateTime to DateTime? so that it accepts null values.
One thing to note is that if you save this value back to a SQL Server but your underlying SQL datatype is datetime instead of datetime2, you will receive an exception since 01/01/0001 00:00:00 is out-of-range in datetime
Further reading:
MSDN recommends using datetime2 in a new development
Difference between value types and reference types explained by Jon Skeet
You do render any controls for property RedordDate so when you post back, the DefaultModelBinder initializes a new instance of ShiftRecord and RedordDate has a value of DateTime.MinValue (1/1/0001).
Add a hidden control for the property to post it back
#Html.HiddenFor(m => m.RedordDate)
Then in the POST method, remove the [Bind] attribute. Currently, even if the value is posted back, it will not bind because it has been excluded from the Include list. You have RecordDate but not RedordDate (a typo?). Note by default all properties will be bound so the attribute is not necessary unless you are specifically excluding properties.
A better alternative is to create a view model that contains only those properties you want to display and edit (What is a view model in MVC) and then in the POST method, get the original data model and map the view model properties to it.
Side note: Can the vehicle be both IsCompanyVehicle and IsOwnVehicle?

Resources