How to build Webapi under WebForms?

Source: Internet
Author: User
Many of the company's projects have been used in the early WebForms. But because of business development, the company to the original project to access the mobile side, webservice a bit old, now more popular restful, so I thought of Webapi.

First, if the new project is the simplest, the file = new + Project =>web=> ASP. NET Web application, under the same time check the Web Forms and Web API core Reference, The Webfroms core and WEBAPI core applications are created.

Second, if the original project on the addition of Webapi, as long as the relevant package reference.

1. Create the WebForms application here first

After the 2.Webfroms project is created, you need to use the NuGet Package Manager for vs. Right-click the reference and select Manage NuGet packages.

Select browse, Search WebApi, select the first Microsoft.AspNet.WebApi, click OK after clicking on the right, select I accept, wait until the output display is successful, 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.

Cleans up the namespace, changes the class to a static type, adds the necessary code, and lacks a self-reference for the reference.

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 Services//WEB API Routing            config. Maphttpattributeroutes ();            Config. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{id}",                defaults:new {id = Routeparameter.optional}            );}}}    

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

protected void Application_Start (object sender, EventArgs e) {//code to run at application startup    Globalconfiguration.configure ( Webapiconfig.register);}

5. Next, let's test a new controller

6. Browser access http://localhost:27650/api/values/get?id=1, test pass.

Third, use Owin to start Webapi as the host

The above is the use of global mode to start WEBAPI, if the project used SIGNALR, you must use Owin as the host, although the online tutorial global can also start signalr (in the Application_Start method plus a sentence RouteTable.Routes.MapHubs (), but Microsoft has declared it obsolete in June 2014, and it is recommended to use Owin startup Class to start signalr. ()

1. Don't say much nonsense, new startup class

2. Create a new Configurewebapi method directly under the configuration method, complete with the following code:

        <summary>///configuration webapi///</summary>///<param name= "app" ></param>public Void Configurewebapi (Iappbuilder app)        {//Create an instance configuration of HTTP httpconfiguration config = new httpconfiguration ();//Map Route            Config. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{id}",                defaults:new {id = Routeparameter.optional}            );//Inject the configuration into the Owin pipeline            app. Usewebapi (config);        }

3. We find that there is an error message Iappbuilder does not contain the definition of USEWEBAPI because of a lack of self-host homestay support, in the Package Manager console, enter the following directive:

Install-package Microsoft.AspNet.WebApi.OwinSelfHost

4. Error message disappears after installation is complete

5. Remove the startup mode from global and initialize the Configurewebapi method.

6. Let's test, http://localhost:27650/api/values/get?id=1, report error 404.

7. The reason is that a package named Microsoft.Owin.Host.SystemWeb is also missing, and this package provides the Owin service to run the ASP. NET request pipeline. In the Package Manager console, enter the following directives:

Install-package Microsoft.Owin.Host.SystemWeb

8. Let's test it again, enter http://localhost:27650/api/values/get?id=1 in the browser, test pass.

Finally, it is worth mentioning that most of the official tutorials use the implicit type var keyword, and some netizens say the benefits of using implicit types are

1. It helps to better name local variables.
2. It facilitates the design of better APIs.
3. It prompts the initialization of the variable.
4. It eliminates the clutter of the code.
5. It does not require a using indicator.

Lou Zhu has no profound experience and research, dare not to add to explain here. Still in the study, the following is the official Microsoft documentation, we feel.

Article to end here, in fact, write how to build Webapi article is also a lot, here is just to do a record, in case you forget, if this article is fortunate to be you see, welcome feel free.

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.