Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle the return of. Net Core API exception information

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

The content of this article mainly focuses on how to deal with the return of. Net Core API abnormal information. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

In C #, if there is an error in the operation, we always use throw to throw an exception, and then the front end will directly show what the exception thrown by the server is after receiving it. Let's take a look at the effect of use before modification.

We directly set the communication time and signature of the verification in the API of. Net Core, if not, throw an exception directly.

C #. Net Core code

The display effect of Android code

In android, we directly see that when rsp.body () returns empty, we directly capture the error message and display it.

Take a look at the runtime effect.

You can see that an exception has come out, but a string of XML messages are displayed after the registration failed.

The exception thrown on the server side is

Or

It's not what we thought, and I can't see what the reason is and where the mistake is. How should we deal with the situation in the air?

Modify the server code

If we want to realize that the exception thrown by the server can be received at the front end, we need to modify it on the server side. Deal with the Configure in the Startup.cs file in. Net Core.

As you can see, the original processing directly jumps to an error page through app.UseExceptionHandler, so the data received is some XML data. Let's renovate this place now.

Let's block the above paragraph first.

Then write a new app.UserExeceptionHandler.

App.UseExceptionHandler (t = >)

{

T.Run (async context = >

{

Var ex = context.Features.Get ()? .error

String msg = ex = = null? "an error has occurred." : ex.Message

Context.Response.ContentType = "text/plain;charset=utf-8"

Await context.Response.WriteAsync (msg)

});

});

And then we recompile and release it.

Let's call it again with the android program to see

You can see that the exception information thrown above can be received and displayed at the front end.

Thank you for your reading. I believe you have some understanding of "how to deal with. Net Core API exception message return processing". Go and practice it. If you want to know more about it, you can follow the website. The editor will continue to bring you better articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report