Showing posts with label ASP.NET Web API. Show all posts
Showing posts with label ASP.NET Web API. Show all posts

Login failed for user IIS APPPOOL\AppPool4.5 or APPPOOL\ASP.NET

The error ‘Login failed for user 'IIS APPPOOL\AppPool4.5’ usually occurs when you configure a new website in IIS or move an existing website to a newer version of IIS.

A simple solution to the error is to add a login to SQL Server for IIS APPPOOL\ASP.NET v4.5 and grant appropriate permission to the database.

Open SQL Server Management Studio > Right click ‘Security’ > New > Login

iis-apppool-login

ASP.NET Web API 2.0 and the new OData keywords

Web API gained OData Support with the Visual Studio 2012 Update 2 release. However it was missing support for some keywords like $select, $expand and $batch. Today we’ll see how to setup our ASP.NET project to use Web API 2.0 beta binaries in Visual Studio 2012 Update 2 and use the new Keywords.

Setting up a WebAPI 2.0 project using Visual Studio 2012

Step 1: We start our project with a Visual Studio 2012 ASP.NET Project. Note that we use .NET Framework 4.5. This is because Web API 2.0 takes dependency of .NET Framework 4.5.

webapi-project-empty-new

Web API 2.0 – Attribute Routing

Web API routing has been pretty flexible using the convention based routing and custom Route Templates. We could specify custom routes, but things got verbose and disconnected for situations where we had to define routes for sub-resources, like for example if you had a list of Books and each book had more than one author, to fetch the authors for a book, you might want a route like http://myapp/api/books/{id}/authors

To do this, we had to define a route in the Route Table as follows

config.Routes.MapHttpRoute(
    name: “AuthorsForBook”,
    routeTemplate: “api/books/{id}/authors”,
    defaults: new { controller = “books”, action=“GetBookAuthors” }
);


Now this works but if we wanted another one, say for a list of Authors, select an author and then the books they have authored, the URL would be http://myapp/api/authors/{id}/books

Creating Editable HTML Table for CRUD operations using Knockout Framework and WEB API

New generation of web applications developed using ASP.NET and ASP.NET MVC can provide excellent user experience with the help of client-side frameworks and libraries like jQuery, Knockout.js. Recently I came across a requirement where we had to implement inline editing and CRUD operations on Tabular data.

The requirement was to display all records in an HTML table, and allow users to add a new row as well as Edit and Delete records. In essence, functionality similar to like what we used to do earlier in ASP.NET GridView, but it had to be done in an ASP.NET MVC application preferably without un-necessary postbacks.

In this sample today, we’ll look at how we can implement this behavior in an MVC 4 application.

Building a Knockout.js based client for Web API services in an ASP.NET MVC app

ASP.NET MVC is now a popular web development platform and often first time users have questions regards to implementation of Data Bound behavior in MVC. This is understandably because most Customers at some point have used ASP.NET Web Forms earlier and are comfortable with the event driven model that it provides.

A common question is how to create an editable list of data allowing users to Edit and Save existing data. The default MVC scaffolding in Visual Studio provides us with a way to implement this where Create/Edit/Delete are implemented on the server side and each action has a separate UI. Today we will see how we can implement it using jQuery and a lightweight client side library knockout.js (also referred to as KO).

On the server, we will use ASP.NET Web API to expose the database services that will be accessed by JavaScript. Needless to say we will use AJAX to communicate with the backend Services.

For any ‘List’ of data there is a need for implementing some kind of template or repeater behavior so that we can define the UI for one row of data and the library/framework takes that template and repeats it for the entire list. We know how to do this in ASP.NET MVC using Razor syntax. Today we’ll see how implement the repeater UI using KO’s templating engine. We will also see how we can define separate templates for Display and Edit modes.

ASP.NET Web API Tracing with Xml Trace Writer

This article was updated for v2.0 and the XML Trace Writer has been added to better understand concepts.

Tracing plays an important part in keeping track of how things are flowing through in your Application Services. Configuring tracing to collate system output is invaluable in gathering correct information about what the service is actually doing other than throwing a HTTP 5xx or HTTP 4xx error.

If you want to implement a TraceWriter of your own, you can implement the ITraceWriter and register it as the required or you could use the Microsoft.AspNet.WebApi.Tracing package from Nuget and simply turn on Tracing.

Before we look into tracing let’s take a sneak peek behind the ‘interesting’ implementation of Tracing in Web API.

Using ASP.NET MVC 4 Mobile Template to build an application using Web API, jQuery and jQuery Mobile

In the last few years, the explosion of powerful Mobile devices combined with their capable operation systems has resulted in a Mobile computing wave that is promising to redefine how we use portable computing devices. Fact is most day-to-day Web 2.0 activities like Social Media updates have moved to mobile platforms in a big way.

As a result of this new wave, Application development targeting mobile platforms is increasingly become the primary targets instead of being add-ons to existing web/mobile presence.
With current device capabilities mobile applications can vary in features, ranging from personal applications like managing SMS/Text, Contacts, Notes, Mails, etc. to Line of Business (LOB) applications like Banking, Financial Management, Order Management etc.

While developing for Mobile platforms is a natural choice, development platform is often conflicted because each mobile platform has its unique set of Tools/IDEs and Development languages. However, one middle path is using HTML5 based JavaScript libraries to build targeted Mobile experiences. ASP.NET MVC with its built in Mobile Development support provides a nice starting point using the jQuery Mobile framework.

Building an Image Flip Viewer as an ASP.NET Web API Service using Knockout JS

Windows 8 introduced a nice component that flips through images using two buttons to the left and right of the control. You can see it the Windows 8 Store when looking at screenshots presented by an app as seen below.

twittelytics-flip-view

I wanted to build one using JavaScript with one requirement that is it shouldn’t load all images at the same time because if you use big images, it kills the page load performance.

With this goal and an ASP.NET Web API backend in mind, I set about building my ‘FlipView’ component in JavaScript.

Uploading Multiple Files through an ASP.NET Web API Service

I had written a post on DotNetCurry on how to upload multiple-files to an Azure Blob through an ASP.NET MVC Web Application. Today we’ll see how we can do the same but store it in a file system instead and use a Web API controller instead, so that the upload service can be hosted anywhere we want. For brevity, we’ll build our Web API sample to use ASP.NET/IIS host.

The API Controller

Step 1: We start off with a Basic Web Application Template and add an empty API Controller to it.

Step 2: Next we add an Upload method to which the Files will be posted. The complete method is as follows:

Implementing Pagination in ASP.NET Web API using OData operators

ASP.NET Web API is a framework that makes it easy to build HTTP services for various types of clients from Microsoft to Non-Microsoft technologies. The new programming model of developing over HTTP is made simple and flexible by using WebAPI. We can design services which can be accessible from a broad range of clients, including browsers and mobile devices.

One of the most frequent requirements while retrieving data using WEB API is that how to implement pagination so that only a subset of data is fetched from the server. To implement this, OData queries with WEB API can be used. You can get more information about the OData Queries from here.

Let us build a simple application that can filter multipage data using OData queries directly.

The Web API Demo

Step 1: Open VS 2012 and create a new Empty MVC application. Name it as ‘MVC40_WEBAPI_Pagination’. Since we will be using jQuery and Knockout.js framework, on this project right click and using ‘Manage NuGet Package’, get the latest jQuery and Knockout script files.

Testing CRUD Operations in ASP.NET Web API using Fiddler

HTTP Services can be used by a broad range of clients including browsers and mobile devices. By using REST [Representational State Transfer] we can build loosely coupled services.

When we think about exposing data on the web, we always talk about four common operations which we use in our day-to-day life –

1. Create
2. Retrieve/Read
3. Update
4. Delete


We call these operations as CRUD operations. HTTP provides 4 basic verbs which we can map to our CRUD operations as described below –

1. POST – CREATE
2. GET – RETRIVE
3. PUT – UPDATE
4. DELETE – DELETE

HTTP Cookies and ASP.NET Web API

HTTP Cookies are bits of information sent by an HTTP Server in an HTTP Response. So when a browser is said to ‘drop a cookie’, it basically implies that the ‘HTTP Client’ (browser in this case) received a Cookie from the server and has persisted it on the client. The ‘HTTP Client’ then returns this cookie to the server via the Cookie Header in subsequent requests.

To dig in just a little bit further, scope of an HTTP Cookie is controlled by attributes set in the cookie header.

- Expires: A Date/Time value indicating when the cookie should expire. The client deletes the cookie once it expires.

- Max-Age: A numeric value representing a timespan span indicating life of the cookie. Like in case of Expires, the cookie is deleted once it reaches it maximum age. Between Expires and Max-Age, Age takes precedence. If neither is set, client deletes the cookie once the ‘session’ expires

- Domain: Specifies the domain which receives the cookie. If not specified, domain is the origin server.

- Path: Limits the cookie to the specified path within the domain. If not specified, the URI’s path is used.

JSON Dates are Different in ASP.NET MVC and Web API

With the advent of ASP.NET MVC and Web API, working over plain HTML as opposed to obtrusive server side ‘web-controls’ has become the norm. Also partial post-backs and AJAX is a must. Among this paradigm shift from WebForms, JSON data transfer has gained tremendous popularity.

We all love JSON it’s easy to understand, light weight and works… almost! We all HATE JSON Dates. Why? Because JSON spec doesn’t say how to handle Dates. This minor detailing has resulted in a lot of head-scratching, a lots of hacks and lot of posts all across the web. I decided to write up a ‘Latest update’ on the JSON Dates Saga. Remember I am using Web API 4.0.2 which is the latest release version.

A special note of thanks to Sumit  Maitra who cracked the JSON serializer difference in MVC 4 and Web API.

Debugging your ASP.NET Web APIs with Fiddler

ASP.NET Web API as we all know, lets us build and expose services over HTTP without the overhead of traditional WS-* based web-services. If you are new to ASP.NET Web API, you can start here. We have also covered some Web API articles on DotNetCurry

Since Web API works over HTTP, the services are easy to interact with even without a dedicated front-end as long as we are sending/receiving required data over HTTP to the correct URL where the service is exposed. Thus we can use HTTP Proxy tools like Fiddler to intercept responses and make requests to the web service.

This is very handy in scenarios when the Services team has a head start over the front-end team who may have not started on the UI development yet. Using a tool like Fiddler, the services team can ensure the web services work as intended.

Getting started with OData and ASP.NET Web API

To paraphrase from odata.org – “The Open Data Protocol (OData) is a Web protocol for querying and updating data. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.”

In plain-speak, OData is a protocol that works over HTTP (the same protocol on which the entire Internet, as we know it, works) and uses pre-established data exchange formats like ATOM (or JSON) to expose data in the Server.

To expose the data, server needs to implement an OData endpoint, which then enables any client application, capable of making HTTP requests, to query this data. That’s of course an over-simplistic scenario as data in server is usually protected and you need to have proper Authorization to access that data. However, the moot point is having an OData endpoint to serve up data locked away in data silos, opens up the data for seamless integration with existing OData clients.