• Complain

Ambily K K - ASP.NET Web API 2: Beginner Guide

Here you can read online Ambily K K - ASP.NET Web API 2: Beginner Guide full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2015, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Ambily K K ASP.NET Web API 2: Beginner Guide
  • Book:
    ASP.NET Web API 2: Beginner Guide
  • Author:
  • Genre:
  • Year:
    2015
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

ASP.NET Web API 2: Beginner Guide: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "ASP.NET Web API 2: Beginner Guide" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

This short Book explains Web API design, concepts, features, and help page generation. This is a starter guide for those who want to quickly understand the basics of Web API. Topics covered in this book are:
Implementing Web API
Web API Client Implementations ASP.NET MVC and jQuery
Scaffolding with Web API Entity Framework
Routing in Web API
Implementing Multiple Serialization Options
Help Page Generation

Ambily K K: author's other books


Who wrote ASP.NET Web API 2: Beginner Guide? Find out the surname, the name of the author of the book and a list of all author's works by series.

ASP.NET Web API 2: Beginner Guide — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "ASP.NET Web API 2: Beginner Guide" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

Contents

About the author
Ambily have been a Microsoft MVP in 2011 and a Microsoft Technology Evangelist . Her focus area includes .NET technologies, Windows 8, Team Foundation Server and HTML5/jQuery. She is also an author for Simple-Talk, CodeProject and dotnetfunda. Her blog is at http://ambilykk.com .
About this book
This short Book explains Web API design, concepts, features, and help page generation. This is a starter guide for those who want to quickly understand the basics of Web API.

Topics covered in this book are:

  • Implementing Web API
  • Web API Client Implementations ASP.NET MVC and jQuery
  • Scaffolding with Web API Entity Framework
  • Routing in Web API
  • Implementing Multiple Serialization Options
  • Help Page Generation
Share your feedbacks, criticisms, and suggestions of improvements to
Introduction
ASP.NET Web API is a framework for building HTTP services that can be accessed from various clients, such as browsers and mobile devices. ASP.NET Web API was introduced as part of ASP.NET MVC 4; however, it has its origins in WCF as WCF Web API. This new HTTP service model is simple to develop and contains common HTTP features, including Caching, Status Code, and so on. In this short book, we will discuss Web API design, concepts, features, and compare Web API with WCF.
Implementing a Web API Project
Lets start our discussion with a sample Web API project. We will be using Visual Studio 2013 as our development environment.

Our first step will be to create an ASP.NET MVC project based on the Web API template, as shown in Figure 1. Figure 1 Creating an ASPNET MVC project based on the Web API template Next - photo 1 Figure 1: Creating an ASP.NET MVC project based on the Web API template Next, well create a sample model inside the model folder in the MVC solution. Right click on the Model folder from the solution explorer and select Add -> Class as shown in Figure 2. Figure 2 Add new Model For this walk-through I am using the Product model - photo 2 Figure 2: Add new Model For this walk-through, I am using the Product model defined in Listing 1. Listing 1: Defining the Product model public class Product { public int Id { get ; set ; } public string Name { get ; set ; } public string Category { get ; set ; } public decimal Price { get ; set ; } } Once the product model is ready, let us create a new API controller inside the controllers folder to process the Product model. By default, an MVC project based on the Web API template adds two controllers: one inherited from the Controller class and the other from ApiController class.

Right-click the controllers folder in Solution Explorer and add a new controller by selecting the Empty API controller option under template as shown in Figure 3. Figure 3 Add Empty API Controller Provide appropriate name for the new - photo 3 Figure 3: Add Empty API Controller Provide appropriate name for the new controller; say ProductsController. Figure 4 Add Controller ProductsController will define two methods for - photo 4 Figure 4: Add Controller ProductsController will define two methods for getting list of products and selected product based on product id. The code for the sample controller should look like that shown in Listing 2. Listing 2: Adding an ApiController class to the sample project public class ProductsController : ApiController { //Define the products list List < Product > products = new List < Product >(); ///
/// Web API method to return list of products /// /// public IEnumerable < Product > GetAllProducts() { GetProducts(); return products; } private void GetProducts() { products.Add( new Product { Id = 1, Name = "Television" , Category = "Electronic" , Price = 82000 }); products.Add( new Product { Id = 2, Name = "Refrigerator" , Category = "Electronic" , Price = 23000 }); products.Add( new Product { Id = 3, Name = "Mobiles" , Category = "Electronic" , Price = 20000 }); products.Add( new Product { Id = 4, Name = "Laptops" , Category = "Electronic" , Price = 45000 }); products.Add( new Product { Id = 5, Name = "iPads" , Category = "Electronic" , Price = 67000 }); products.Add( new Product { Id = 6, Name = "Toys" , Category = "Gift Items" , Price = 15000 }); } ///
/// Web API method to retrurn selected product based on the passed id /// /// /// public IEnumerable < Product > GetProducts( int selectedId) { if (products.Count() > 0) { return products.Where(p => p.Id == selectedId); } else { GetProducts(); return products.Where(p => p.Id == selectedId); } } } Run the project and access the API by appending the uniform resource locator (URL) with /api/Products , as in http://localhost: 59509/api/Products . This api call will return the list of Products.

We can also access the GetProducts method by passing the SelectedId argument as part of the query string: http://localhost:59509/api/Products?SelectedId=2 . By default Web API returns JSON data, so when you run the Web API directly from browser, it will prompt to save or open the JSON output. Following Figure shows the api output for the /api/Products request. Figure 5 Web API Response - JSON format Passing Complex Objects to a Web - photo 5 Figure 5: Web API Response - JSON format

Passing Complex Objects to a Web API Method
Passing a simple value is a straightforward process in Web API, but in most cases youll need to pass a complex object as a method parameter. You can pass these objects either by using the [FromUrl] or by using [FromBody] attribute. The [FromBody] attribute reads data from the request body.

However, the attribute can be used only once in the method parameter list. Let us understand the importance of using complex parameters using the code snippet in Listing 3, which expects a complex object, Time , as its input. Listing 3: Passing a complex object as a method parameter public class SampleController : ApiController { ///
/// Get the time based on passed parameters /// /// /// public string GetTime( Time t) { return string .Format( "Received Time: {0}:{1}.{2}" , t.Hour, t.Minute, t.Second); } } public class Time { public int Hour { get ; set ; } public int Minute { get ; set ; } public int Second { get ; set ; } } Now, let us try to pass the values to the GetTime method using the Hour , Minute and Second values. This will throw the Object reference not set to an instance of an object exception. Even though we have mentioned the values related to the fields of the Time object, API method is not able to map the values properly to the parameter. Now modify the code to include the [FromUri] attribute so it can handle the complex object from query string.

As you can see in the following snippet, we include the [FromUri] attribute before specifying the Time object: public string GetTime([ FromUri ] Time t) { -------------------} Invoke the web API method with the same values before and observe the result, as shown in Figure 5. Figure 6 Receiving successful results when calling the GetTime method Web - photo 6 Figure 6: Receiving successful results when calling the GetTime method

Web API Client Implementations
Web API can be consumed from different applications including web applications, client side technologies like jQuery and AngularJS, native mobile applications and so on. This section discuss about how we can invoke Web API from various client applications; ASP.NET MVC and jQuery.
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «ASP.NET Web API 2: Beginner Guide»

Look at similar books to ASP.NET Web API 2: Beginner Guide. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «ASP.NET Web API 2: Beginner Guide»

Discussion, reviews of the book ASP.NET Web API 2: Beginner Guide and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.