Monday 21 January 2013

Tricky Asp.net interview Question

 Hi friends,

In the last month I had given one interview in one of the large MNC in Pune.
In that interview, one interviewer asked me one questions about Try Catch block in asp.net.
I liked that questions and really it’s challenging question.

Scenario 1 :-

Please find following Code and trace the output


Try{
Response,redirect("1.aspx");
}

Catch{
Response,redirect("2.aspx");
}

finally{
Response,redirect("3.aspx");
}

Answer:

The Output of this one is that it will redirect the page to 3.aspx as on try block this will throw a redirect error but after catch block the control will go to the finally block which will redirect the page to 3.aspx..





Scenario 2

Trace the output



Try
{
Server.Transfer("1.aspx");
}

Catch
{
Server.Transfer("2.aspx");
}

finally
{
Server.Transfer("3.aspx");
}

Answer:

In the Second case we are using Server.Transfer which will directly transfer the current page to show the contents of the page to be redirected keeping the url address as the same.. IN this case it will move through all three blocks and would show the contents of 1.aspx, 2.aspx and 3.aspx from the main page... ie., if we are transferring from main.aspx then it will show the contents of 1.aspx plus the content of 2.aspx and the content of 3.aspx in main.aspx..





Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------


Wednesday 9 January 2013

Advantages for consuming RESTful services



1.     What is Rest

REST service is based on HTTP protocol. Each method invocation is a http get,post,delete or put request. Since it is HTTP protocol based so anything that can talk http can consume your service without much effort i.e. javascript, C#, Java, Whatever.
Also REST call results can be cached like normal http pages (by intermediate proxies or client machine) if you send the right caching parameters with the response.
However it is also more oriented towards 'resources' while normal WCF service is oriented towards RPC style communication.
Normal WCF supports callbacks and whole lot of other things that REST doesn't support but obviously it comes with cost of platform compatibility and complexity.

2.     What is Advantages of consuming RESTful services

  1. ·         REST is powerful, intuitive, and relatively easy to implement.
  2. ·         Light weight – not lot of extra xml markup as in SOAP.
  3. ·         Unlike SOAP it provides much simpler result that can be human readable.
  4. ·         It is very easy to build no extra toolkits are required.
  5. ·         Improve scalability of the application through caching and session state.
  6. ·         Custom URIs using URI templates
  7. ·         Consistency with design of the World Wide Web
  8. ·         Lightweight - not a lot of extra xml markup
  9. ·         Human Readable Results
  10. ·         Easy to build - no toolkits required
  11. ·         Each operation in a service is uniquely identified.
  12. ·         Each operation has their, own unique URI and can be accessed via this URL all across the web. 
  13. ·         Each URL is an object representation.
  14. ·         The Object can retreived using HTTP GET.
  15. ·         The Object can be deleted using a POST or PUT, use DELETE to modify the object
  16. ·         It is firewall friendly and it is fairly simple and straight forward.



3.     More About RESTful services

  1.  The Restful web services are nothing but a web programming model to expose WCF Service.
  2.  REST can have more impact on data transfer reduction in combination with JSON or other serialization techniques.
  3. REST stands for Representation State Transfer. Representation State Transfer term is invented by one of the original author of the HTTP protocol, Roy Fielding in his 2000 doctoral dissertation.  Visit the wiki  for more about REST.



4.     KEY CONCEPTS OF REST

  1. Resources: Resources are nothing but entities in a business domain for ex.  Items, Vendors, Customer etc…
  2. URIs: All resources are referenced using URIs (Universal Resource Identifiers)
  3. Representations: It consist of 2 things Data that reflects current or desired state, meta   data that provides description of data values. For ex. HTML, XML, JSON, Text files, zip, Images, Mpegs etc…


5.     Example


                [OperationContract(Name = " AddTwoNumber ")]
        [WebInvoke(UriTemplate = "/", Method = "POST")]
         int AddTwoNumber(int i, int j);

       

        [OperationContract(Name = " SubTwoNumber")]
        [WebGet(UriTemplate = "/")]
        int SubTwoNumber(int i, int j);


Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------