. net # 6 simplified webapi tutorial and demonstration of hot overloading and automatic code Decompilation



The. Net6lts version has been released for several days. Here’s a question about using Net6 to develop a simplified version of webapi (minimalapi) introductory tutorial, and vs2022 above the two powerful new skills (hot overloading, automatic code decompilation) accompanying demonstration.

1. Create a new project. Here it is named someexample:

2. Choice Net6 version, and the HTTPS configuration and the option to use the controller are removed here:

3. After creation, the original screen is as follows. Moreover, only the program file is left in the solution column, and other files have been streamlined. And there is no main function entry, namespace, etc.

4. Next, let’s start with a few simple examples of webapi methods. First delete the original and replace it with a custom webapi method, a get and a post. The specific code is shown in the figure below.

5. After running, the built-in swagger page will be opened. Bigwigs can compare some features of the code part and the swagger part by themselves.

6. The execution result of the HelloWorld get method is shown in the following figure.

7. The execution result of the post method of hellomiapai is shown in the following figure.

8. According to different uses of API, we can also classify it on the swagger display. Classification is realized by setting tags. Specific setting content: set withtags at the end of the method, which specifies the display name of the classification. After running swagger, the display effect and relevant code are shown in the figure below.

9. Manage API methods by code files. If there are many API methods, it is unrealistic to write them all in the program. So let’s expand. Create a new static class testservice for testing, and implement a static method testapi. The parameter of the method must be thiswebapplication. Then you can expand the implementation of webapi through the passed in parameters. For example, an API method of serviceapi is added here, as well as other implementations, as shown in the figure below.

10. After adding the expanded API, it needs to be referenced in the program file. The details are shown in the figure below.

11. Display the swagger after running.

12. The execution result under the test returns the expected result, indicating that the test is successful.

13. Test the function of vs2022 thermal overload, modify it in the return value, and then click the small flame button of thermal overload for thermal update.

14. After the hot update, there is no need to recompile. Call the API method just set on the swagger display page directly. The call result is shown in the figure below, indicating that the hot overload update is successful. When encountering the code that needs debugging in the future, the big guys can easily carry out hot update without recompiling, which can greatly improve the efficiency.

15. Next is the verification test of dependency injection. Add a new test service class testdependencyinjection. The specific implementation content is shown in the figure.

16. Itestdependency injection content of the interface corresponding to testdependencyinjection:

17. In the program file, register the above services. A single instance is registered here. The specific code is shown in the figure below.

18. The next step is to verify whether the dependency injection is successful. Here I inject in the webapi method of serviceapi. Precautions for injection: in the delegate type parameters of webapi method, the first parameter must be httpcontext; Then the parameters are followed. Here is the interface of the injection service. And the show method is called in the API method for verification.

19. Run the program, click to execute the corresponding webapi method on the swagger display page, and view the results. As shown in the figure below, the content in the show method is output, which represents the success of dependency injection.

20. Extend the hidden skills of vs2022: when referring to a third-party or nuget package (not limited to. Net version), press F12 on the corresponding encapsulated class or method to directly enter the decompiled code and see the encapsulated source code. For example, take useswagger as an example. Press F12 here to see what happened.

21. Normally, we should go in F12. We can only see the attribute name and method name, but not the specific implementation. Now you can see the specific implementation code. As shown in the figure below, there are specific codes in the method. You can also continue to F12 to see the underlying code.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.