This article is a mirror article of machine translation, please click here to jump to the original article.

View: 61280|Reply: 6

[Source] ASP.NET WebApi uses MessageHandlers to unify the return interface model format

[Copy link]
Posted on 10/28/2020 1:29:28 PM | | | |
Scenario: When the frontend calls the backend API, we want the backend to return a unified JSON model interface, and the data returned by different interfaces is wrapped in the data field of the unified interface.

The original approach was to return the same model object for all interface settings, which could be done, but it was a bit verbose.

In the ASP.NET WebAPI, a full HTTP request, the flowchart is as follows:



My own understanding of MessageHandlers is the Middleware middleware in ASP.NET Core.

For example:

It turns out to return json content:

["value1","itsvse.com"]
We would like to return the following format:

{"success":true,"message":"ok","data":["value1","itsvse.com"]}
All interface return models contain fields such as success, message, etc.

Unified response entity model


Create a new ApiHandler object with the following code:

Register a custom MessageHandlers in the WebApiConfig configuration file with the following code:

Use the default ValuesController controller in the new project, and modify it as follows:

Try accessing our API interface, the rendering is as follows:



(End)





Previous:CentOS 8 graphical interface and command line toggle
Next:A service based on .NET Core on Linux
 Landlord| Posted on 2/23/2021 5:17:21 PM |
jQuery intercepts the results returned by Ajax
https://www.itsvse.com/thread-9570-1-1.html
Posted on 7/13/2021 6:20:54 PM |
test
Posted on 3/8/2022 1:30:50 PM |
Learn to learn
Posted on 6/5/2022 10:34:47 PM |
test1test1
Posted on 9/24/2022 2:01:07 PM |
Prompt:Authors are banned or removed content is automatically blocked
Posted on 11/28/2022 9:26:50 PM |
This is exactly what I want
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com