Monthly Archives: May 2015

ASP.NET and IIS Integration IIS 6.0 and II6.0+ (IIS 7.0, IIS 7.5, IIS 8.0…)

In these earlier releases, IIS processed a request to an ASP.NET content type and then forwarded that request to the ASP.NET ISAPI DLL, which hosted the ASP.NET request pipeline and page framework. Requests to non-ASP.NET content, such as ASP pages or static files, were processed by IIS or other ISAPI extensions and were not visible to ASP.NET.

The major limitation of this model was that services provided by ASP.NET modules and custom ASP.NET application code were not available to non-ASP.NET requests. In addition, ASP.NET modules were unable to affect certain parts of the IIS request processing that occurred before and after the ASP.NET execution path.
aspnet-integration-with-iis6.0

In IIS, the ASP.NET request-processing pipeline overlays the IIS pipeline directly, essentially providing a wrapper over it instead of plugging into it.

IIS processes requests that arrive for any content type, with both native IIS modules and ASP.NET modules providing request processing in all stages. This enables services that are provided by ASP.NET modules, such as Forms authentication or output cache, to be used for requests to ASP pages, PHP pages, static files, and so on.

The ability to plug in directly into the server pipeline allows ASP.NET modules to replace, run before, or run after any IIS functionality. This enables, for example, a custom ASP.NET Basic authentication module that is written to use the Membership service and SQL Server user database to replace the built-in IIS Basic authentication feature that works only with Windows accounts.

In addition, the expanded ASP.NET APIs use direct integration to enable more request-processing tasks. For example, ASP.NET modules can modify request headers before other components process the request, by inserting an Accept-Language header before ASP applications execute, which forces localized content to be sent back to the client based on user preference.

aspnet-integration-with-iis-7-Integrated-Mode

Because of the runtime integration, IIS and ASP.NET can use the same configuration to enable and order server modules, and to configure handler mappings. Other unified functionality includes tracing, custom errors, and output caching.

When you are creating app pool in IIS 7.0 and above, you get following options / modes for integrating ASP.Net Pipeline

  • Classic
  • Integrated

AppPool-Integrated-Classic-Mode

  • Classic mode (the only mode in IIS6 and below) is a mode where IIS only works with ISAPI extensions and ISAPI filters directly. In fact, in this mode, ASP.NET is just an ISAPI extension (aspnet_isapi.dll) and an ISAPI filter (aspnet_filter.dll). IIS just treats ASP.NET as an external plugin implemented in ISAPI and works with it like a black box (and only when it’s needs to give out the request to ASP.NET). In this mode, ASP.NET is not much different from PHP or other technologies for IIS.
  • Integrated mode, on the other hand, is a new mode in IIS7 where IIS pipeline is tightly integrated (i.e. is just the same) as ASP.NET request pipeline. ASP.NET can see every request it wants to and manipulate things along the way. ASP.NET is no longer treated as an external plugin. It’s completely blended and integrated in IIS. In this mode, ASP.NET HttpModules basically have nearly as much power as an ISAPI filter would have had and ASP.NET HttpHandlers can have nearly equivalent capability as an ISAPI extension could have. In this mode, ASP.NET is basically a part of IIS.

JSON-P Mystery

What is JSONP?
JSONP stands for ‘JavaScript Object Notation with Padding’.

For e.g.

// JSON: 
{
    'name' : 'Mark',
    'age' : 33,
    'hobbies' : ['running','cycling','climbing'],
    'temper' : 'moody'
}

//JSONP
newPerson( 
{
    'name' : 'Mark',
    'age' : 33,
    'hobbies' : ['running','cycling','climbing'],
    'temper' : 'moody'
})

As you can see, the only difference is that JSONP format requires some type of callback function with the object passed as a parameter whereas regular JSON is just a regular inline JavaScript object (the theory behind JSONP is that the callback name is specified by the requester (within the query string), not by the party which receives the request).

So, imagine you have a ASP.NET MVC Application on one domain (www.microsoft.com) which outputs JSONP like that shown above. Now to invoke that JSON P call in your domain (www.fabrikam.com) you would use the script tag like shown below

<!-- www.fabrikam.com -->
<script type="text/javascript" src="www.microsoft.com/Home/Index"></script>

Home/Action – It is controller action which returns reponse like newPerson({..}).

In the Fabrikam domain Index.html page would have the global function newperson()

function newPerson(o) {
    alert(o.name);
}

The above code will alert the name of the person defined in the JSONP data

The purpose of JSONP is to overcome the boundaries of the infamous same-domain-policy which restricts XHR requests to the same domain meaning that you cannot make Ajax requests to other domains. There’s no need to worry about that with JSONP because it doesn’t even require Ajax to work; all it’s doing is using script tags and callbacks…

Issues with JSONP

  • There is no error handling for JSONP calls. If the dynamic script insertion works, you get called; if not, nothing happens. It just fails silently. For example, you are not able to catch a 404 error from the server. Nor can you cancel or restart the request. You can, however, timeout after waiting a reasonable amount of time
  • It can be quite dangerous if used with untrusted services. Because a JSONP service returns a JSON response wrapped in a function call, which will be executed by the browser, this makes the hosting Web application vulnerable to a variety of attacks. So JSPNP calls should be made to trusted sources
  • You cannot do POST operation using JSONP, only GET is supported

SQL Joins – Inner, Outer, Left Outer, Right Outer. How to Explain?

Consider Following 2 tables for the example for various types of Joins

Customer
CustomerId Name Phone
1 John Doe 609-937-9819
2 Mary Blag 609-937-9818
3 Mark Waugh 609-937-9861
Order
OrderID OrderDate Custid
Order1 03/12/2014 2
Order2 01/12/2014 3
Order3 03/05/2014
  • Inner Join – The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. Syntax:
    SELECT column_name(s)
     FROM table1
     INNER JOIN table2
     ON table1.column_name=table2.column_name;
    
    Or
    SELECT column_name(s)
     FROM table1
     JOIN table2
     ON table1.column_name=table2.column_name;
    

    innerjoin

    SELECT Customer.CustomerId, Customer.Name, Orders.OrderId, Orders.OrderDate 
    FROM Customer 
    INNER JOIN Orders ON Customer.CustomerId = Orders.CustId 
    
    Inner Join Query Result
    CustomerId Name OrderId OrderDate
    2 Mary Blag Order1 2014-03-12 00:00:00.000
    3 Mark Waugh Order2 2014-01-12 00:00:00.000
  • LEFT Outer Join – The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
    Syntax:

    SELECT column_name(s)
                FROM table1
                LEFT JOIN table2
                ON table1.column_name=table2.column_name
    
    Or
    SELECT column_name(s)
                FROM table1
                LEFT OUTER JOIN table2
                ON table1.column_name=table2.column_name
    

    left outer join

    SELECT Customer.CustomerId, Customer.Name, Orders.OrderId, Orders.OrderDate 
    FROM Customer 
    LEFT OUTER JOIN Orders ON Customer.CustomerId = Orders.CustId 
    

     

    Left Outer Join Query Result
    CustomerId Name OrderId OrderDate
    1 John Doe NULL NULL
    2 Mary Blag Order1 2014-03-12 00:00:00.000
    3 Mark Waugh Order2 2014-01-12 00:00:00.000
  • RIGHT Outer Join – The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
    Syntax:

    SELECT column_name(s)
                FROM table1
                RIGHT JOIN table2
                ON table1.column_name=table2.column_name
    
    Or
    SELECT column_name(s)
                FROM table1
                RIGHT OUTER JOIN table2
                ON table1.column_name=table2.column_name
    

    right outer join

    SELECT Customer.CustomerId, Customer.Name, Orders.OrderId, Orders.OrderDate 
    FROM Customer 
    RIGHT OUTER JOIN Orders ON Customer.CustomerId = Orders.CustId 
    
    Right Outer Join Query Result
    CustomerId Name OrderId OrderDate
    2 Mary Blag Order1 2014-03-12 00:00:00.000
    3 Mark Waugh Order2 2014-01-12 00:00:00.000
    NULL NULL Order3 2014-03-05 00:00:00.000
  • FULL Outer Join – The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
    Syntax:

    SELECT column_name(s)
                FROM table1
                FULL OUTER JOIN table2
                ON table1.column_name=table2.column_name
    

    full outer join

    SELECT Customer.CustomerId, Customer.Name, Orders.OrderId, Orders.OrderDate 
    FROM Customer 
    FULL OUTER JOIN Orders ON Customer.CustomerId = Orders.CustId 
    
    Full Outer Join Query Result
    CustomerId Name OrderId OrderDate
    1 John Doe NULL NULL
    2 Mary Blag Order1 2014-03-12 00:00:00.000
    3 Mark Waugh Order2 2014-01-12 00:00:00.000
    NULL NULL Order3 2014-03-05 00:00:00.000