First test of. NET web development WebApi and first test of. netwebwebapi

Source: Internet
Author: User

First test of. NET web development WebApi and first test of. netwebwebapi

I saw it a few days ago. net ef (Entity Framework), found that there is such an advanced thing, as long as the operation of several classes can complete the database addition, deletion, query, modification, and can be directly exported using the database class (DB First), you can also use classes to generate a database (Code First), which is too advanced.

Previously, EF was used as an API Service website. Every time you want to add a method, you need to parse the incoming QueryString in the View, and then call the Controller to perform addition, deletion, query, modification, and modification to the database. In fact, I have never heard of WebAPI before, although it has never been used. I checked it out today to fully meet my application requirements-ASP. NET Web API is a framework used to easily build HTTP services that can be accessed by a variety of clients (including browsers and mobile devices), similar to the open interfaces of various manufacturers.

  • Preparation materials:

VS 2015 community edition (Web development tools installed)

  • Procedure

Follow the wizard.

Select WebAPI, and then wait a moment. VS will help you create a project with built-in routines.

Select the Controllers folder, right-click to add "controller" DefaultController, and select a WebAPI empty controller.

Add the following code:

Public class DefaultController: apiController {// <summary> /// set an object // </summary> /// <param name = "aa"> A </param> /// <param name = "bb"> B </param> // <returns> </returns> [HttpGet] public Exception Set (string aa, int bb = 0) {return new Exception (aa + bb );} /// <summary> /// Get a value /// </summary> /// <returns> floating point </returns> public float Get () {return 2 ;}}

Then we press Ctrl + F5 to run the command. Well, we can get a home page like this. It seems that there is nothing, Mo Fang. Click "API" in the upper left corner to go to the list of all APIs. The APIs and controllers are generally one-to-one. At the end, we can find a class we wrote and the two functions in it. This is too convenient, saving my previous View-layer parsing work and allowing the client to remotely call classes on the server.

I. Notes

As you can see, we have just clearly written XML comments, but the APIDescriptionWhy is it empty? This Help Page must have this function. To enable this function, perform the following operations:

1. generate an XML comment File
Call out the project property-"generate" and mark "A" here. (If your class or method has no comments, you will be reminded, if you don't like it, enter 1591 in B)

2. Let HelpPage read XML
Go to Areas/HelpPage/App_Start/HelpPageConfig. cs, remove the first two comments of the Register method, and fill in your XML Path in the following path. OK.2. Set to return Json by default

After talking about it for half a year, let's take a look at the calling effect, GlobalConfiguration. configuration. formatters. xmlFormatter. supportedMediaTypes. clear (); // json GlobalConfiguration is returned by default. configuration. formatters. jsonFormatter. mediaTypeMappings. add (new QueryStringMapping ("datatype", "json", "application/json"); // select datatype as the return format to replace GlobalConfiguration with any parameter. configuration. formatters. xmlFormatter. mediaTypeMappings. add (new QueryStringMapping ("datatype", "xml", "application/xml "));

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.