How to determine if a certain model is bound from request body or from route or from query parameters, etc. in ASP.NET Core - 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?

Related

ASP.NET Transfer data from controller action

There is a sales service implemented as a Telegram bot. I need to create a website control panel for this service. Since the service is a .NET application I am thinking to use ASP.NET Core technology.
How do I transfer data from the controller action to the Program class containing all the functionality of the service (maybe it is worth defining the Program as a static class)?
You may have misunderstood Asp.Net Core. .net core adopts the pipeline mode, that is, when you call the action in the controller, it will enter the middleware pipeline of Program.cs(.net 5 is Startup.cs), and execute in sequence according to the order of your middleware, adopting the principle of first in, last out. This means that if you follow the normal .net core logic, the value you get in the controller (except the parameters defined in the URL), you cannot pass it into Program.cs. When you successfully enter the action of the controller, Program.cs has been executed.
Not sure what your sales service looks like, but I think you can register it as a service and use it in your controllers using dependency injection.
Helpful link: ASP.NET Core Middleware.

Query String Parameter Fails to Populate .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

ASP.NET Core 6 register global exception filter from library

In my ASP.NET Core 6 application, I usually register filters inside the call to builder.Services.AddControllers by adding to the Filters array. If I'm writing a reusable library, and that library wants to automatically register a filter, how would I do that? While I can of course tell consumers to manually add the filter themselves, I'd rather have it in a registration extension on the IServiceCollection object.
Back in the WebAPI days, for example, there was a GlobalConfiguration type object that could be used.

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

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.