DTO gets lost in transmission POST to REST API? - angular9

My object is being lost in transmission. I (1) enter some text, (2) submit a form and (3) see the correct POST and (3) I see the objects construction is correct and (3) that the http.post method is being used. However on the ServerSide I see (4) that the controller is receiving a null object. Please help!
Using Angular 9, Ionic 5's http client... very straightforward... .NET Core 3.1 c# backend...

Actually I needed to add the [ApiController] attribute to the controller.

Related

How to DRY [FromBody] attribute and default content-negotiation in ASP.NET Core MVC on all post actions?

We have more than a thousand HTTP services across 50 applications all of which use HttpPost for operations that change server's state.
We use Fiddler to test each service we create and two boilerplate piece of code is always bothering us and prevent smooth coding.
All models (reference types) should have [FromBody] attribute in order to be bound to HTTP Request's body
Each time we should include Content-Type: application/json in our HTTP Requests for content-negotiation.
Since more than 99 percent of our services use JSON in body for POST operations that need a model to be bound, how can we DRY these two pieces of code in ASP.NET Core MVC? In other words, how to tell ASP.NET Core MVC that always perform [FromBody] for reference types if the HTTP method is POST, and how to instruct it to use only JSON content-negotiation for HTTP Request's body?
Short answer: use [ApiControllerAttribute], but it has some other limitations and features.
Starting with asp.net core mvc 2.1 you can decorate your specific controller, base controller or an entire assembly with the attribute [ApiControllerAttribute]. It alters the behavior of your controller in many ways, one of which is exactly what you need: the binding will look into the body by default, so you don't need to specify [FromBody] on each Action.
Be aware that it adds some other features that you may or may not need:
Attribute routing requirement: you will need to specify routing using attributes on all the controllers decorated with the [ApiControllerAttribute].
Automatic HTTP 400 responses: that's it automatic model validation before your action is called.
Multipart/form-data request inference when decorating your action with [FromForm].
Problem details for error status codes: more detailed information is returned together with the status code.
However, all those features (other than attribute routing) are not hard enforced and can be changed as you need.
More information here: https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.0#multipartform-data-request-inference
and here: https://www.strathweb.com/2018/02/exploring-the-apicontrollerattribute-and-its-features-for-asp-net-core-mvc-2-1/

Validating JSON request in Interceptor vs in Controller

I recently started working on Rest Based Web services. Here I have a requirement where I need to validate the contents of the request based on a parameter of request.
My question is What will be the best approach to do this validation.
I have two different JSON requests hitting my webservice.
Validate in the interceptor. This will need me to cast the request object back to its actual Type.
Validating the request in Controller.
Also I would like to know if filters can be used in this scenario and what benefit will it give me.
Thanks.

call json web api nopcommerce

Hi I am new for nopcommerce 3.5. I need to write a restful web service api to third party(for eg mobile) access the service. I know that we can access through Nop.Plugin.Misc.WebServices . I had enable the service from administrator site.
But now is my question. How can i call the web service for eg GetPaymentMethod , product list and etc
And if I want to write my custom web service by using web api. what is step to create? I cant find any documentation about the web service. Please guide me some example
Thanks
If you want a really quick start in writing a web service in NopCommerce, you can follow the MVC architecture and:
Create an Action method inside a Controller that you find appropriate for your purpose. For example, if you want access to a product list, you might create an Action inside CatalogController that follows the logic of the existing ProductList action.
Set up a Route in RouteProvider.cs to point to the Action you created. Based on this route you can deduce the URL of your service.
Do the processing that you need inside the Action. If this Action/service is to be called with parameters (in query string format: param=value&param2=value2), you can just put these parameters in the Action's header:
public ActionResult QuickService(int param, string param2) { ... and .NET will take care of having them initialized.
Store results in an object (can also be an anonymous object) and at the end of your action, return it as Json: return Json(resultsObject); Again, ASP.NET takes care of the JSON serialization and automatically sets the Content-Type HTTP response header to "application/json".
You can consume the service calling the URL that corresponds to the route of your Action.
If you want users to be able to log in, by using the above method, it gets a little bit trickier. You need the webservice client to be able to accept and send cookies, as well as make appropriate services for Login, Logout, Register,...
However, in this case, you might be better off with a mobile version of the site.
Final note: If you don't want to alter base NopCommerce code, you can apply the steps above to a plugin. The majority of NopCommerce plugins follow the MVC architecture, so you can apply the steps above.

bindingResult.getAllErrors() vs bindingResult.getFieldErrors()

I am developing a application using Spring MVC 3.0 frame work, I have following requirement,
There are multiple form in one jsp page. I am using ajax to submit each form. But after submitting, each form will go to different controller. In controller I will validate input data if there is any error I need to send validation result back to jsp page. Right now I am storing errors into a separate list and sending back to jsp through json response. I am not sure whether to use bindingResult.getAllErrors() or bindingResult.getFieldErrors() to get list of errors in my controller. What's the difference between both?
getAllErrors()
By using bindingResult.getAllErrors you will get all errors, both
global and field ones.
getFieldErrors()
By using bindingResult.getFieldErrors() you will get all errors associated with the given field.
Here is a useful Link that may help you understand difference between each better.
getAllErrors() returns all errors, both Global and Field. getFieldErrors() only returns errors related to binding field values. I am not sure what a "Global" error is generated from, as I have never seen one.

How can I get DotNetNuke 6.2 Service Framework to modelbind json data

DotNetNuke Serviceframework is based on ASP.NET MVC 2, and therefore does not include json modelbinding out of the box.
I've tried a number of approaches:
MVC3 jsonvalueprovider
custom json model binder
custom value provider
The code to register these was called, however the methods on these objects themselves were not called.
Registering these is an interesting area in itself as in DotNetNuke, we don't have access to the global.asax file.
I've also attempted to deserialize the request input stream, in the the controller, but I get a nullreferenceexception there, and I get the correct data size, but all nulls!
Any ideas?!
Ok,
I have a workaround that is clean and functional.
I'm using a jquery plugin from here .This converts json into a standard forms form post for you.
using this with jquery & knockout looks like this:
$.ajax({
url: '<%= ModulePath %>Api/Register/Search',
type: 'POST',
data: $.toDictionary(ko.mapping.toJS($root),"",true),
success: function (data) { //do something });
Leaving the question open, in case anyone has any ideas to get json working directly.
You need to register a JSON value provider to get this to work. See http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx for details.
The best way to register the value provider is to do it in your route mapper. Be sure to guard the registration to ensure it occurs only once, as the route mapper is occasionally called more than once. If you are going to do this in a module deployed to servers you don't control you should probably inspect the contents of the factories collection to ensure no other service has already registered the value provider.
Services Framework in DNN 7 is based on WebAPI and natively supports JSON, so this hassle will go away soon.