Query String Parameter Fails to Populate .NET Core - asp.net-core

I am upgrading an app from MVC to .NET Core. The ruleSetId parameter works in MVC, but not in .NET CORE. Why is ruleSetId = when I sent 203389?
POST https://localhost:44313/Customer/RuleSet/CreateRule?ruleSetId=203389

Use [FromQuery] attribute for ruleSetId and for other parameter you want get it from body use [FromBody]
See this link about Model Binding

Related

How to disable the model property validation of not-nullable properties?

I'm using FluentValidation in an ASP.NET Core 6 Web API project. This works fine for most cases, so:
Request body JSON syntax validation is done by ASP.NET Core.
Request object property validation is done by FluentValidation.
But there is one specific case that is problematic:
If the request type contains a not-nullable property (e.g. string instead of string?) and the request object contains a null value for it, validation is done by ASP.NET Core (but should be done by FluentValidation).
My current workaround is to annotate all that not-nullable properties with [ValidateNever] so that ASP.NET Core ignores them, but this is not nice.
Is there a way to disable ASP.NET Core model property validation of not-nullable properties?
Note: I can't disable ASP.NET Core validation completely because then it won't even return validation error results for JSON syntax errors.
try to set as below :
builder.Services.AddControllersWithViews(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
The problem has been explained in this document:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.suppressimplicitrequiredattributefornonnullablereferencetypes?view=aspnetcore-6.0

How to determine if a certain model is bound from request body or from route or from query parameters, etc. in ASP.NET Core

I am using ASP.NET Core 5.0 with .NET 5.0 on Windows. One thing I need to do is from a ActionFilter to determine if a particular action argument is bound using request body. Is there a way to inspect the data source of the action method argument?

ASPNET MVC filter, WebAPI filter, ASPNET core MVC filter, are three different things?

Filters in ASP.NET confuse me. I have seen 3 kinds of filters:
MVC filter System.Web.Mvc.IAction​Filter
WebAPI filter System.Web.Http.Filters.IActionFilter
Core MVC filter Microsoft.AspNetCore.Mvc.Filters.IActionFilter
Is there a Core WebAPI filter?
I have a WebAPI filter which uses Dependency Injection via constructor parameters. It works well in my WebAPI application which uses Unity as IOC container.
container.Register<IMyService, MyService>();
container.Register<LogActionFilter>();
var filterInstance = container.Resolve<LogActionFilter>();
GlobalConfiguration.Configuration.Filters.Add(filterInstance);
The following attribute Log is defined as a simple attribute which only has three properties. The LogActionFilter above will check if an action has the Log attribute using GetCustomeAttribute<LogAttribute>().
[Log(Enabled=true, Level=2, Format="xxxx")]
public void MyAction()
{
}
Now I want to migrate it to ASP.NET CORE. Can I use DI for CORE filters?
I only find Microsoft.AspNetCore.Mvc.Filters.IActionFilter in ASPNET CORE. And it seems hard to use DI. Still don't know how to register such a filter.
I know there is ServiceFilter(typeof(XxxFilter)) but it's not good to pass paramters like [Log(Enabled=true, Level=2, Format="xxxx")]
Anyone has an example of CORE filter?
Thanks.
TL;DR: They are all pretty much the same thing.
System.Web.Mvc.IAction​Filter is an MVC action filter from ASP.NET MVC.
System.Web.Http.Filters.IActionFilter is an MVC action filter from ASP.NET MVC WebAPI.
Microsoft.AspNetCore.Mvc.Filters.IActionFilter is an MVC action filter from ASP.NET Core.
So the common theme is that they are all MVC action filters. That means that they run around the execution of an action within the context of an MVC framework.
The difference is just that they are used for different MVC frameworks.
ASP.NET MVC is the older MVC framework running on the .NET Framework. WebAPI is the framework that was developed specifically to create APIs for the web. It is generally similar to ASP.NET MVC but is still a separate entity.
With ASP.NET Core, the new and current open source MVC framework by Microsoft, the distinction between “MVC” and “WebAPI” was removed and instead you just have a single MVC framework included within ASP.NET Core. So you just have ASP.NET Core, and there you happen to use action filters around MVC actions.
There is very good documentation on MVC filters for ASP.NET Core. There is also a section explicitly about dependency injection in filters. You can use the ServiceFilter or TypeFilter to properly use dependency injection within your filters. ServiceFilter is used when you want to resolve the filter completely from the DI container, while you can use TypeFilter to also provide some parameters directly in the attribute. The example on TypeFilter ironically also uses a logging example.
That being said, logging is generally not the best use case for action filters since ASP.NET Core actually does log around the execution of actions by default.

What is the purpose of AddFormatterMappings() on Asp.net Core 2

I am evaluating the configuration of a Web API App with Asp.Net Core 2. To configure I know it is better for my project to use .AddMvcCore() rather then use AddMvc() since I don't need Razor as we can see here.
Now, I am not sure if I also need this .AddFormatterMappings(). So my question is what is it for?
You can see from the source code in the MVC GitHub repo that it adds a FormatFilter service into the DI setup. The summary for the FormatFilter class is as follows:
A filter that will use the format value in the route data or query
string to set the content type on an ObjectResult returned from an
action.

ASP.NET Core ValidationAttribute message language

How can I globally overload error messages getting ,for example, from [Required] attribute? Probably, I should change CultureInfo, but I can't find any tutorials. Language still English as default. Thank you!
A first reference on globalization and localization of ASP.NET Core is https://docs.asp.net/en/latest/fundamentals/localization.html.
There you'll find information about how to translate data annotations and how to configure ASP.NET Core to select the appropriate language / CultureInfo for each request.
DataAnnotations error messages are localized with IStringLocalizer<T>.
Using the option ResourcesPath = "Resources", the error messages in
RegisterViewModel can be stored in either of the following paths:
Resources/ViewModels.Account.RegisterViewModel.fr.resx
Resources/ViewModels/Account/RegisterViewModel.fr.resx
For built-in error messages for identity models (like "This email is already taken.) or basic validation texts you have to do some more configuration. I have documented some work in ASP.Net Core localization.
You can try using ErrorMessageResourceName and ErrorMessageResourceType, using resource files to store messages.