WebAPI construction (1) how to build WebAPI and webformswebapi under Webforms

Source: Internet
Author: User

WebAPI construction (1) how to build WebAPI and webformswebapi under Webforms

Many of the company's projects have been using WebForms in the early stage. However, due to business development, the company needs to access the mobile end on the original project. webservice is a bit outdated. Now RESTFul is popular, so we almost thought of WebAPI.

1. If it is the easiest to create a project, file => New => project => Web => ASP.. NET Web applications. Check Web Forms and Web API core references at the bottom. The webfroms core and WebAPI core applications are created.

 

2. If a WebAPI is added to an existing project, you only need to reference the related package.

1. Create a WebForms Application

2. After the Webfroms project is created, use the NuGet Package Manager of. Right-click reference and choose manage NuGet packages.

Select Browse, search for WebAPI, and select the first Microsoft. AspNet. WebApi. Click "Install" on the right and click "OK". Then select "accept". After the output is displayed successfully, the installation is complete.

 

3. Right-click the Web Project, add a folder named App_Start, and create a cs file named WebApiConfig under the App_Start folder.

Clear the namespace, change the class to static type, add necessary code, and reference is missing.

The complete code is as follows:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. http; namespace WebFormsDemo {public static class WebApiConfig {public static void Register (HttpConfiguration config) {// Web API configuration and service // Web API route config. mapHttpAttributeRoutes (); config. routes. mapHttpRoute (name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter. optional });}}}

4. You need to register WebAPI in Application_Start method under the Global. asax file. here you need to reference System. Web. Http. The complete code is as follows:

Protected void Application_Start (object sender, EventArgs e) {// code GlobalConfiguration. Configure (WebApiConfig. Register) that runs when the application starts );}

5. Next let's test and create a Controller

6. Access http: // localhost: 27650/api/values/get? Id = 1. The test is successful.

 

3. Use OWIN as the host to start Webapi

The preceding steps start WebAPI in Global mode. If SignalR is used in the project, OWIN must be used as the host, although there is a Global tutorial on the Internet, you can also start SignalR (add a RouteTable in the Application_Start method. routes. mapHubs ();), but Microsoft declared it out of date as early as June 2014. We recommend that you useOwin Startup ClassTo start SignalR. (Http://go.microsoft.com/fwlink? LinkId = 320578)

1. No more nonsense. Create a Startup class.

2. Create a ConfigureWebapi method directly under the Configuration method. The complete code is as follows:

/// <Summary> /// configure Webapi /// </summary> /// <param name = "app"> </param> public void ConfigureWebapi (IAppBuilder app) {// create an HTTP instance and configure HttpConfiguration config = new HttpConfiguration (); // map the route config. routes. mapHttpRoute (name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter. optional}); // inject the configuration into the app in the OWIN pipeline. useWebApi (config );}

 

3. We found that the error message IAppBuilder does not contain the UseWebApi definition, because Self-Host boarding support is missing. In the Package Manager Console, enter the following command:

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

4. The error message disappears after the installation is complete.

5. Remove the Global startup method and initialize the ConfigureWebapi method.

6. Let's test http: // localhost: 27650/api/values/get? Id = 1, Error 404 is reported.

7. The reason is that a package named Microsoft. Owin. Host. SystemWeb is missing, which provides the Owin service to run the ASP. NET network request pipeline. In the Package Manager Console, enter the following command:

install-package Microsoft.Owin.Host.SystemWeb

8. Let's test it again. Enter http: // localhost: 27650/api/values/get in the browser? Id = 1. The test is successful.

Finally, it is worth mentioning that most of the official tutorials use the implicit type var keyword. Some netizens say that the advantages of using the implicit type are:

1. It facilitates better naming local variables.
2. It facilitates the design of better APIs.
3. It promotes variable initialization.
4. It eliminates code confusion.
5. It does not require the using indicator.

Without profound understanding and research, I dare not explain it here. The following is an official Microsoft document.

The article is over here. In fact, there are many articles on how to build a WebAPI. Here we only make a record to prevent you from forgetting it. If you are lucky enough to see this article, please do not hesitate.

Related Article

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.