Creating an ASP.NET Web API Project

The ASP.NET Web API services are built on the basic nature of HTTP: GET and POST. In this way, you can send a request to a service the same way you would type a uniform resource locator (URL) into your browser. This request can pass parameters on the query string. Of course, you can get a response from the service, too. You can also post data to a service, work with Secure Sockets Layer (SSL) for security, and do most of the basic web-like things you would do in any website.

You can add ASP.NET Web API services to any ASP.NET web application. The services can be hosted on the same server and domain as another website. (Of course, there are other hosting options, too.) This means you can define a Web API service inside your ASP.NET MVC sites, Razor web page sites, single-page applications (SPAs), and ASP.NET Web Form sites. To do so, you simply right-click the website in Solution Explorer and choose Add, New Item. From here, you select the Web API Controller Class template. The version of the Web API Controller template you use is dependent on the version of the ASP.NET application stack your project targets. If you’re using the prior version of ASP.NET, for example, the template is called Web API Controller Class (v2.1). If you’re using ASP.NET 5.0, the template is simply called Web API Controller Class. We leverage this template in a moment.

Microsoft has unified the MVC and Web API frameworks with ASP.NET 5.0. However, Visual Studio has a separate template for Web API projects based on the ASP.NET 5.0 framework application stack. This template is accessed by creating a new ASP.NET web application. Recall that you do so via File, New, Project and selecting ASP.NET Web Application. This brings up the secondary dialog for selecting a specific web template, as shown in Figure 19.3. In this case, you select the Web API template.

Image

FIGURE 19.3 Select the ASP.NET 5 Web API template to create a project for writing REST-based services.

Notice in Figure 19.3 that there are a number of ASP.NET templates. You can create web services using all these templates. There is a Web API template targeted at using the ASP.NET 4.6 application stack to create HTTP services. There is another for ASP.NET 5. The ASP.NET 5 Web Site template includes everything you need to create Web API services that target the new 5.0 application stack. This template, as discussed in Chapter 17 and 18, includes the basics of a website, too.

Figure 19.4 shows the Solution Explorer for a new project using the ASP.NET 5 Web API template. Notice that the project contains a Controllers folder; this is where you write your Web API methods following the ASP.NET MVC pattern. The figure also shows the referenced libraries of the DNX Core 5.0 framework relative to service applications. Not shown is Startup.cs; this includes the ASP.NET request pipeline configured for MVC and the HTTP request pipeline.

Image

FIGURE 19.4 Start with the ASP.NET 5 Web API template for building a project for just REST-based services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset