[Experience sharing] Application of return type Jsonmessage in Webapi

Source: Internet
Author: User

This is a unique good type, a good type that you can't put up with, OK, don't rip, directly on the dry.

I believe we all know that the return of JSON data when invoking an interface has become an unwritten standard, because of its fast parsing, easy to read and other excellent features, so the vast majority of the program apes love.

Let's introduce a return type that you might ignore or not know about today Jsonmessage

1     /// <summary>2     ///return Message3     /// </summary>4      Public classJsonmessage5     {6         /// <summary>7         ///is successful8         /// </summary>9          Public BOOLSuccess {Get;Set; }Ten         /// <summary> One         ///result Encoding A         /// </summary> -          Public stringCode {Get;Set; } -         /// <summary> the         ///Result Message -         /// </summary> -          Public stringMessage {Get;Set; } -  +          Public Override stringToString () -         { +             returnJsonhelper.tojson ( This); A         } at}

The above is the whole code of Jsonmessage, so we can see that you do not have to bother to consider what parameters I should use to set my return information, this has prepared a unified and comprehensive parameter return value.

Direct on Demo

1         /// <summary>2         ///Demo3         /// </summary>4         /// <returns>JSON-formatted information</returns>5 [HttpPost]6[Route ("Api/login/demo")]7          Publicjsonmessage Demo ()8         {9Jsonmessage jmtemp =Newjsonmessage ();Ten             Try One             { AJmtemp.code =" $"; -Jmtemp.message ="Demo Success"; -Jmtemp.success =true; the}Catch(Exception ex) -             { -Jmtemp.code ="201"; -Jmtemp.message ="Demo failed"; +Jmtemp.success =false; -             } +             returnjmtemp; A}

Let's see how the show works.

This is the end, isn't it simple

==================================================

Reward him:

[Experience sharing] Application of return type Jsonmessage in Webapi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.