I have a .NET 6 Web API project where a model User class looks like this:
public class User
{
[MinLength(3, ErrorMessage = "UserName needs to be greater than 3 characters")]
public string Username { get; set; } ...
I have a controller that uses the User model to register a user account:
[HttpPost("register")]
[ServiceFilter(typeof(ValidationFilterAttribute))]
public async Task<ActionResult<ServiceResponse<int>>> Register(UserRegisterDTO request)
and I've added a ServiceFilter with a custom ValidationFilter class:
public class ValidationFilterAttribute : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new UnprocessableEntityObjectResult(context.ModelState);
}
} ...
Finally, in my Program.cs, I've added the ValidationFilterAttribute prior to adding my controllers:
builder.Services.AddScoped<ValidationFilterAttribute>();
builder.Services.AddControllers();
I've debugged my code and clearly see it going into the OnActionExecuting Filter method but the ModelState.IsValid is always valid, even when I enter a username of 2 characters.
Am I missing something on how this data annotations and validation works? Examples online always seem to imply the validation and error messages appear on the GUI but I had assumed if invalid values were sent in the API payload, it would be detected in this manner and you could return a response that the object can't be processed, along with the details of what failed.
I would like to ensure clean data at the data entry level and not have to explicitly code data validation in the object methods.
This seems to apply to both, azure functions and ASP.NET Core Web API: Assume that you have a simple class:
SomeClass
{
propA: string;
propB: string;
}
In the Controller you add something like this
... [FromBody]SomeClass data
The runtime will now instantiate a object named data with the json values provided in the body.
However, if you provide this json:
{
"propA": "SomeText"
}
it will still instantiate propB with an empty string, although no value is present in the body. Now the question would be: Is it possible to suppress that behavior, so not to set propB if there is no value provided?
You can decorate your string properties with the [Required] data annotation.
[Required]
public string A { get; set; }
This does however enable empty strings like:
{
"propA": ""
}
The data annotation [Required] has an extra property: AllowEmptyStrings. If set to false the above property will not validate.
Do take in mind these validation annotations cause 400 statuscodes in your responses.
The following documentation illustrates how to use the Options Pattern in ASP.NET Core to create a strongly-typed options class to access JSON configuration data.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
This C# class
public class MyOptions
{
public string Option1 { get; set; }
public int Option2 { get; set; }
}
represents a portion of this JSON configuration file (the first two root-level properties)
{
"option1": "value1_from_json",
"option2": -1,
"subOptions": {
"subOption1": "subvalue1_from_json",
"subOption2": 200
}
}
I want to add another C# property named SubOptions to the MyOptions class that returns the raw data of the subOptions JSON sub-section, without creating a strongly-typed class for that sub-section of the JSON configuration file, but I don't know what data type to use (or if it's even possible to do that).
If I use string, I get a runtime error when service.Configure<MyOptions>(Configuration); is called, saying System.InvalidOperationException: 'Cannot create instance of type 'System.String' because it is missing a public parameterless constructor.
If I use object or dynamic, I get a different runtime error when service.AddSingleton(cfg => cfg.GetService<IOptions<MyOptions>>().Value); is called to register an instance of the MyOptions class, saying System.ArgumentNullException: 'Value cannot be null. Parameter name: type'
If I use JObject, I get {} back when I access the SubOptions property of the MyOptions object that's injected into my API Controller.
I know I can convert the sub-section to a JSON string property by escaping the sub-section data, but I want to avoid treating the sub-section as a string, and instead leave it as raw JSON.
Is it possible to do what I want to do? Is there a data type that works with the Options Pattern that will allow me to access the JSON sub-section without having to create a strongly-typed class?
*For background, I'm trying to create an API Controller method that returns the content of the JSON sub-section to the API client. I want to avoid using a strongly-typed class for the sub-section, so that the JSON configuration file can be edited on the server, adding new properties and values to the sub-section that will be returned to the API client, without having to update the C# code and redeploy the API service. In other words, I want the JSON sub-section to be 'dynamic', and just pull it and send it to the client. *
You can sorta do get raw configuration object by forcing your SubOptions property to be of IConfigurationSection:
public class MyOptions
{
public string Option1 { get; set; }
public int Option2 { get; set; }
public IConfigurationSection SubOptions { get; set; } // returns the "raw" section now
public string SubOptions_take2 { get; set; }
}
so you would still bind your strongly typed object in your Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MyOptions>(Configuration);
...
}
but this is where luck appears to run out, because even though it is a whole section - as far as options binder is concerned it's all been deserialised and parsed into hierarchy of values already. There appears to be no easy way to reassemble it back into one string. Injecting IOptionsMonitor allows you to get the values by opting for .GetChildren() but I could not find an obvious way to get the whole hierarchy without writing custom code to just recursively walk it (which I will leave out for you to play with should you feel this is worth the effort):
public IndexModel(IOptionsMonitor<MyOptions> options)
{
_options = options.CurrentValue;
var subOptions = _options.SubOptions as ConfigurationSection;
var children = subOptions.GetChildren(); // you see, the config has already been parsed into this hierarchy of items - it's too late to get the raw string value
var s = JsonConvert.SerializeObject(children);
// will produce something like this JSON:
//[{"Path":"SubOptions:subOption1","Key":"subOption1","Value":"subvalue1_from_json"},{"Path":"SubOptions:subOption2","Key":"subOption2","Value":"200"}]
}
one way around it will be to actually encode your json as string in the config file:
"subOptions_take2": "{\"subOption1\": \"subvalue1_from_json\",\"subOption2\": 200}"
then you can just grab it later:
public IndexModel(IOptionsMonitor<MyOptions> options)
{
_options = options.CurrentValue;
var subOptions_string = _options.SubOptions_take2;// this is valid json now: {"subOption1": "subvalue1_from_json","subOption2": 200}
}
I guess, you can use JObject from Newtonsoft.Json package - it's the default JSON parser & serializer in Asp.Net Core
I've created an Azure Function and I'm running it locally:
[FunctionName("HttpTriggerCSharpSet")]
public static async Task<HttpResponseMessage> Set([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] MyDocument req, TraceWriter log)
{
// ...
}
Notice that MyDocument is the first parameter instead of HttpRequestMessage. I've read in the documentation that this approach should work, and it seems very similar to ASP.NET model binding (in my mind, anyway). MyDocument is a POCO with just 3 properties.
public class MyDocument
{
public string Name { get; set; }
public int ShoeSize { get; set; }
public decimal Balance { get; set; }
}
When I POST to the function like so (I'm using Postman):
I get an error message: [8/8/2017 2:21:07 PM] Exception while executing function: Functions.HttpTriggerCSharpSet. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'req'. System.Net.Http.Formatting: No MediaTypeFormatter is available to read an object of type 'MyDocument' from content (which you can also see in the screenshot of Postman above)
I've tried form-data and x-www-form-urlencoded and even raw from Postman, same error every time. I've also tried switching back to HttpRequestMessage and using req.Content.ReadAsAsync<MyDocument>, and I get a similar error. Am I constructing my POST incorrectly, or am I writing my Azure Function incorrectly. In either case, what's the correct way?
Make sure to specify the header:
Content-Type: application/json
then the following body should work for your code:
{
"Name": "myUserName",
"Balance": 123.0,
"ShoeSize": 30
}
I was working on ASP.NET MVC web API, I'm having this error:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
My controller is:
public Employee GetEmployees()
{
Employee employees = db.Employees.First();
return employees;
}
why I m getting this error?
For me this was a problem with circular referencing.
The accepted answer did not work for me because it only changes the behaviour of the JSON formatter, but I was getting XML when I called the service from the browser.
To fix this, I switched off XML and forced only JSON to be returned.
In the Global.asax file, put the following lines at the top of your Application_Start method:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
Now only JSON results will be returned. If you need XML results, you will need to find a different solution.
in your global.asax file, in the Application_start() method add this line:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
I hope that helps you!
I got the same problem. And I solved it. I put the default constructor to the DTO class.
Ex:
public class User
{
public User()
{
}
}
Hope it work with you!
Put this in constructor. Hope this solve the problem:
public MyController()
{
db.Configuration.ProxyCreationEnabled = false;
}
I found two solutions to this. The first and easiest to implement is to change any IEnumerables, ICollections to a type of List. The WebAPI can serialize this objects, it however cannot serialize interface types.
public class Store
{
[StringLength(5)]
public string Zip5 { get; set; }
public virtual List<StoreReport> StoreReports { get; set; } //use a list here
}
The other option is to not use the native JSON serializer and run this override in the Register method of the WebApi Config:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
Solution is simple.
After LINQ query add .ToList() (or ToDictionary if need).
It will do eager loading than lazy loading of the data
**
this bug occur when calling from request web api/wcf/... from client side, but as side effect, you will need to include depending relations by include keyword.
**
public CustomerPortalContext()
: base("Name=CustomerPortalContext")
{
base.Configuration.ProxyCreationEnabled = false;
}
If you are working with EF, besides adding the code below on Global.asax
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
Dont`t forget to import
using System.Data.Entity;
Then you can return your own EF Models
please check the web api documentation for this problem, Handling Circular Object References
Regards
If you use web api with Entity Framework, a solution can be
Failed to serialize the response in Web API with Json
Basically, you need to create a model corresponding to each EF model, this removes dependencies between classes and allow easy serialization.
Code: (taken from the referenced link)
Create a UserModel
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Change my method GetAll()
public IEnumerable<UserModel> GetAll()
{
using (Database db = new Database ())
{
List<UserModel> listOfUsers = new List<UserModel>();
UserModel userModel = new UserModel();
foreach(var user in db.Users)
{
userModel.FirstName = user.FirstName;
userModel.LastName = user.LastName;
listOfUsers.Add(userModel);
}
IEnumerable<UserModel> users = listOfUsers;
return users;
}
}
Default Entity 6 use XML to apis, in your project, find the file "Global.asax" File and add this line:
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
This line remove the XML Formatter.
but if you found this problem with other entities/classes, you have to create a new DTO for each class, and if you have a lot of them, you can find a problem, also I think that create a DTO only for solving this problem is no the best way...
Did you try this?
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.All;
Regards
hmmm, Following may help.
I was getting the same exception, and in my case I was passing the actual poco entity created for entity code first. Since, it contains relation with other entities, I just created the viewmapper/dto entity on top of it to return.
It works fine now.
Poco Entity:
public class Tag
{
public int Id{get;set;}
public string Title{get;set;}
public IList<Location> Locations{get;set;}
}
ViewMapper/Dto
public class TagResultsViewMapper
{
public int Id{get;set;}
public string Title{get;set;}
//just remove the following relationship
//public IList<Location> Locations{get;set;}
}
Your question is quite similar to mine. You must not return data from database directly. For this, you must create Model and associate data you want show.
In my example, There are data about User that Json couldn't serialize, I had create a userModel and, in my API, I return userModel instead User from database.
The logic of convert or associate data between User and UserModel must be in API.
Failed to serialize the response in Web API with Json
This was the specific error I was getting back from my odata Web API call:
The 'ObjectContent`1' type failed to serialize the response
body for content type 'application/json; odata.metadata=minimal'.
I finally figured out that my dbContext class had a poorly formatted table name being assigned in onModelCreating.. so the SqlClient was dying looking for a table that didn't exist in my db!!