Used together, Visual Studio 2015 and WDK 10 provide an integrated development environment for creating efficient, high-quality drivers for devices running Windows 10. Create Web API project. Create Web API project. Here, you will learn how to create a new ASP. NET Web API project using Visual Studio. You can create a Web API project in two ways. Web API with MVC Project. Stand- alone Web API Project. Visual Studio 2. 01. Web includes Web API template which creates a new Web API project with ASP. NET MVC application and includes all the necessary references to get started. In this article I go through step-by-step how to install MVC 4 in Visual Studio 2010. I already have a project which is ready to build. Currently, I am using visual studio 2013. But, I don't know how to create an MSI setup in visual studio 2013, but. This article introduces the configuration of the OWIN Startup Class and how various startup classes work in VS 2013 RC. For this, open Visual Studio 2. Web and click on File menu and click on New Project. This will open New Project popup as below. Create Web API project. In the New Project popup, expand Visual C# and select Web node in the left pane. Select ASP. NET Web Application template in the middle pane and enter the name of a project and click OK. This will open New ASP. NET Project popup as shown below. Select Web API Template. Select Web API in the above popup. Notice that this has selected and disabled MVC and Web API checkboxes. It means that it will add necessary folders and references for both MVC and Web API. Click Ok to create a new Web API + MVC project as shown below. Web API project. This project is same as default MVC project with two specific files for Web API, Web. Api. Config. cs in App_Start folder and Values. Controller. cs in Controllers folder as shown below. Web API project. The Web. Api. Config. cs is configuration file for Web API. You can configure routes and other things for web API, same like Route. Config. cs is used to configure MVC routes. It also creates Web API controller Values. Controller. cs by default. You will learn about Web. Api. Config and Controller in the next section. Thus, you can create Web API project with MVC to get started on your application. Stand- alone Web API Project: Here, we will create a new stand- alone Web API project without MVC project. For this, open Visual Studio 2. Web - > go to File menu and select New Project. This will open New Project popup as below. Create Web API Project. Select Web template in the left pane and ASP. NET Web Application in the middle pane. Enter a name of the project, location and Solution name as shown above. Clicking on OK will open a popup as shown below. Select Project Template. In the above popup, select Empty as a template and click ok. This will create an empty "Hello. Web. API" project. Now, we need to add latest Web API references using Nu. Get Package Manager. Right Click on the project and click Manage Nu. Get Packages. as shown below. This will open Manage Nu. Get Packages popup. Select Online in the left pane and search for webapi (make sure that internet connection is on). This will display all the Web API related packages. Select Microsoft ASP. NET Web API2. 2 package and click on Install as shown below. Install Web API Package. Accept the license agreement and continue. Install Web API Package. The following Web API packages are displayed upon successful installation. Web API Packages. Now, create Controllers and Configuration folder in the Hello. Web. API project as shown below. We will add Web API controller in the Controllers folder and configuration class in the Configuration folder. Web API Project. Now, add a new class in the configuration folder and name it "Hello. Web. APIConfig" with following content. You may give any appropriate name). Example: Web API Configurationusing System. System. Collections. Generic. using System. Linq. using System. Web. using System. Web. Http. namespace Hello. Web. API. Configuration. Hello. Web. APIConfig. Register(Http. Configuration config). Web API routes. config. Map. Http. Attribute. Routes(). config. Routes. Map. Http. Route(. name: "Default. Api". route. Template: "api/{controller}/{id}". Route. Parameter. Optional }. Now, add Global. Add - > click New Item. Add New Item popup as below. Select Global Application Class and click OK. Add Global. asax. This will add Global. We need to configure our Web API routes when application starts. So call Hello. Web. APIConfig. Register() method in the Application_Start event in the Global. Global. asaxpublicclass. Global : System. Web. Http. Application. Application_Start(object sender, Event. Args e). Global. Configuration. Configure(Hello. Web. APIConfig. Register). Web API is configured only using code based configuration using Global. Configuration class. The Configure() method requires a callback method where you have configured your Web API. Thus, when application starts it will call Application_Start event which in turn calls Hello. Web. APIConfig. Register() method to configure your Web API. Now, let's add Web API controller by right clicking on the Controllers folder - > select Controller. Add Web API Controller. Select Web API in the left pane and Web API 2 Controller - Empty in the middle pane and click Add. This will open another popup to enter a name of your controller as below. Enter controller name and click Add. Enter Controller Name. This will create following empty Hello. Controller class in Controllers folder. Example: Web API Controllerusing System. System. Collections. Generic. using System. Linq. using System. Net. using System. Net. Http. using System. Web. Http. namespace Hello. Web. API. Controller. Hello. Controller : Api. Controller. Now, we need to add action methods. Here, we will add simple Get action methods as shown below. Example: Web API Controller using System. System. Collections. Generic. using System. Linq. using System. Net. using System. Net. Http. using System. Web. Http. namespace Hello. Web. API. Controller. Hello. Controller : Api. Controller. publicstring Get(). Hello World". Now, compile and run the project and navigate to http: //localhost: xxxx/api/hello in the browser. It will display following result. Access Web API in the Browser. So in this way you can create a simple Web API from scratch with config and controller class. During the development, programmers often need to test and check the result of Web API. Learn how to test Web API for development in the next section.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2018
Categories |