Pages

Wednesday, February 16, 2011

Post JSON to MVC Controller with XDomainRequest, XmlHttpRequest and jQuery Ajax Call

Once I try to post a JSON to a MVC controller, I use jQuery.post method and all works fine as you can see in this site, the first in the below references.

But when I try to use XDomainRequest or XmlHttpRequest, the JSON parameter at server side has null properties, in others words, the data is sent but not serialized to our serializable Type.

So, in order to receive data at server side, i used the DataContractJsonSerializer class to read the Stream sent in the Request, like this:





Of course, to make use of DataContractJsonSerializer, we need a class representing JSON with [DataContract] attribute and respective [DataMember] just like a WCF service for instance.

Then, the code at server side, looks like this:































And, at client side, making use of XDomainRequest and XmlHttpRequest Objects, and jquery the code is






















This way, you have a MCV controller that can receive JSON using the JSON serializing capacities of MCV3 from XDomainRequest, XmlHttpRequest and jQuery ajax call.


References:
http://lozanotek.com/blog/archive/2010/04/16/posting_json_data_to_mvc_controllers.aspx
http://msdn.microsoft.com/en-us/library/bb412170.aspx
http://msdn.microsoft.com/en-us/library/bb412179.aspx

Monday, February 14, 2011

Javascript - Consume Service With Cross Domain Request

Every time you need to do a request over your domain, you have to be aware of what kind of browser is running your code. If is an IExplorer, you will need to use the object XDomainRequest because this object is specifically to make cross-domain requests.

Here's a sample of usage:









If you want to make it complaint with others browsers you will need to use the XmlHttpRequest object, or, even simpler, making an ajax call with jQuery. This object, together with CORS, make possible to make cross domain requests.

The snippet of XmlHttpRequest goes like this:







And the call using jQuery:













XDomainRequest is the response by Microsoft against XmlHttpRequest with CORS protocol, instead of integrate it (CORS) with IExplorer, they create their own object for cross domain's requests.

Regarding CORS, all you have to do, is making your service server aware for OPTIONS verb and return the needed headers, because the browser itself makes the magic happens. So, to enable cross domain ajax call, at server you need to...



















With all this set up, you should have your communication with your server on. One little note, be aware of kind of service and object you are using, for instance, XDomainRequest only accept text/plain to send (xdr.send(string)) and an WCF service accepts XML, JSON, etc. but not text/plain.


References:
http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
http://stackoverflow.com/questions/4739384/xdomainrequest-problem
http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
http://www.codeproject.com/KB/ajax/jQueryWCFRest.aspx

Friday, February 04, 2011

WIF - "Thread was being aborted" Error When SignIn / SignOut

If you ever try to implement WIF (code name Geneva), you probably found the error "Thread was being aborted" at the moment you try to execute the:

FederatedPassiveSecurityTokenServiceOperations.ProcessSignInResponse(responseMessage, response);
OR
FederatedPassiveSecurityTokenServiceOperations.ProcessSignOutRequest(requestMessage, User, requestMessage.Reply, Response);

This happens because in the middle of Windows.IdentityModel there's an Response.Redirect("returned page") instead of Response.Redirect("returned page", false) causing the error above mentioned. This is an known issue, and is save to ignore it. So, making use of a catch, you can jump the error and your code would be like...












You can skip this error and hoping for an hotfix soon :)


References:
http://p2p.wrox.com/asp-net-1-1/8403-error-thread-being-aborted.html
http://rmencia.wordpress.com/2010/05/05/federated-signin-requires-federated-signout/
http://social.msdn.microsoft.com/Forums/en/Geneva/thread/b248fafd-f5aa-47f7-9c7a-1ecaa944d2c0

Wednesday, February 02, 2011

C# - Get All Properties From Class Or Type [Reviewed]

Looking to the previous post, with an excellent advice from Pedro Lamas (Thanks a lot!!!), the source code now goes like this...





















Now, making use of reflection and extension method together, we have the possibility to use the GetPropertiesAndValues method at any object inside the program scope.

In other words, you can use GetPropertiesAndValues for every object (like an instance of a class) present in your program, getting the returned string "My object Properties & Values \n\nPropName: PropValue\n (etc.)"

For instance:



















References:
http://stackoverflow.com/questions/1447308/enumerating-through-an-objects-properties-string-in-c
http://www.developer.com/net/csharp/article.php/3592216/Using-the-New-Extension-Methods-Feature-in-C-30.htm

Tuesday, February 01, 2011

C# - Get All Properties From Class Or Type

Have you ever try to get all properties from a Class/Type on the fly? the next snippet demonstrates how to query the class instance and retrieve the properties names and their respective values.

It goes like this...




















This way you have a string returned like "propName1: valueProp1\n" and so on.


Reference: