HttpHanlder Vs HttpModule

HttpHandler: 

1.They are process which run when a request is made by client. Handlers include extension like (.aspx,.ashx).

2.We can implement custom Handler by implementing IHttpHanlder (ProcessRequest and IsReusable method)

Life cycle of HTTPHandler is only till the request is made.

HTTPModule : 

1.They are events which can be used even before page load /request is made to the server. 2.HTTP Module can be used for the entire life cycle of MVC where as HTTPHandler is valid only for handling request.

3.We can implement custom Module by implementing IHTTPModule(Init and Dispose Methods)

 

 

#Content Type vs Accept in Request Header

Accept : Tells the server  what the media type client needs as response.

“Accept”: text/html —Client expects the response as text/html.

Content-Type : Tells the server what media type the client sends when making a request.This is useful for server so that it can parse the request accordingly.

“Content-Type” :application/json –Client tells the server that it sending input as json.

 

#Content Negotiation in Web API?

A client might send a request with multiple media types in header

<pre class="prettyprint prettyprinted"><span class="pln">GET http</span><span class="pun">:</span><span class="com">//localhost.:21069/api/products/1 HTTP/1.1</span>
<span class="typ">Host</span><span class="pun">:</span><span class="pln"> localhost</span><span class="pun">.:</span><span class="lit">21069</span>
<span class="typ">Accept</span><span class="pun">:</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">json</span><span class="pun">,</span><span class="pln"> text</span><span class="pun">/</span><span class="pln">javascript</span><span class="pun">,</span> <span class="pun">*</span><span class="com">/*; q=0.01</span></pre>

Accept : Tells the server whats the client want as response.

In the above case client tells,it is comfortable with josn,xml or any type.

The server response with

<pre class="prettyprint prettyprinted"><span class="pln">HTTP</span><span class="pun">/</span><span class="lit">1.1</span> <span class="lit">200</span><span class="pln"> OK
</span><span class="typ">Content</span><span class="pun">-</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">json</span><span class="pun">;</span><span class="pln"> charset</span><span class="pun">=</span><span class="pln">utf</span><span class="pun">-</span><span class="lit">8</span>
<span class="typ">Content</span><span class="pun">-</span><span class="typ">Length</span><span class="pun">:</span> <span class="lit">57</span>
<span class="typ">Connection</span><span class="pun">:</span> <span class="typ">Close</span>

<span class="pun">{</span><span class="str">"Id"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"Name"</span><span class="pun">:</span><span class="str">"Gizmo"</span><span class="pun">,</span><span class="str">"Category"</span><span class="pun">:</span><span class="str">"Widgets"</span><span class="pun">,</span><span class="str">"Price"</span><span class="pun">:</span><span class="lit">1.99</span><span class="pun">}</span></pre>

The server makes a choice of its own of 3 types and returned the json in first place.

The process of deciding the content type between the server and client is called content -negotiation.

 

 

#Web API-Custom MediaTypeFormatters

1.You can create custom MediaTypeFormatter by inheriting abstract   BufferedMediaTypeFormatter. 

2.To add it to your web api,make changes in HttpConfiuration in Startup.cs

<pre class="prettyprint prettyprinted"><span class="pln"> config</span><span class="pun">.</span><span class="typ">Formatters</span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="kwd">new</span> CustomFormat <span class="pun">());</span> 
</pre>

3. BufferedMediaTypeWritters  has two methods WriteToStream(write in custom format(Serialize)) and ReadFromStream(read the custom format(deserialize)).


public class CustomFormat :BufferedMediaTypeFormatter
{
public CustomFormat()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/csv"));
}
public override bool CanReadType(Type type)
{
throw new NotImplementedException();
}

public override bool CanWriteType(Type type)
{
throw new NotImplementedException();
}
}

# Linq To Entities DBFunctions and SqlFunctions

When using linq to sql queries,few functions like DateTime Comparison,FirstorDefault would not work and you would end up getting exception “LinqtoEntities is not supported by this function”.

Problem:

For instance I was trying to compare a column of Date-time type with today’s date,I had issue when comparing it since the sql server Date-Time type was of (yyyy-mm-dd) and today’s date was of (mm-dd-yyyy),I was not able to use ToString method to format date since linq to entities  did not support it.

Solution:

LinqToEntities provides DBFunction and SqlFunction for sql related operations.It has got several methods like DateAdd,DatePart,TruncateTime etc…I had used the same to compare dates.


var somequery = from f in mytable where DbFunction.TruncateTime (f.mydatecolumn) == Dbfunction.TruncateTime(SqlFunction.GetDate())

 

 

# What is REST ?

Rest(Representational State Transfer)  is recommended style/suggested pattern  which is recommend for web applications.

A System which is Restful  is  expected to meet the five principles listed below :

1.System must be represented as resource.

2.Uniform Interface (Usage of GET,POST,PUT,DELETE ) is advised to be used across the system.

3. Identify Resource by a Unique Identifier.

4. Communication via Representation (represent data by JSON/XML etc..)

5. Stateless.

# Web API : HTTP VERBS PUT VS POST

We would be aware  that both Put and Post could do both insert and update operation.However we must follow certain standard and choose either of  one verb based on scenario.

Use POST :

1.If  the insert is going to create a new resource/identifier than use POST.

2.Post is not idempotent.

what is Idempotent ?

Idempotent :  Let’s assume we are sending a  request to server with post method  “/create/1” .If we send  multiple request,it would have side effects like creating the resource twice. This might not be the desired output. Other verbs like GET,PUT are Idempotent. If we make a request to server with GET Methods ” /GET/1″ multiple times,it will yield the same result and the same with PUT  “/PUT/1”,it will only update  existing record.

 

POST /resources HTTP/1.1
The post will create new resource
/resources/newresource/

 

Use PUT :

1. If insert or update has to be performed based on existing resource .

2.Put is idempotent.

PUT /resource/<perform operation on existing_resource> 

//Update Resource or create related resource if not existing.