Are there any good tools for integrating GWT with ASP.Net? [closed] Are there any good tools for integrating GWT with ASP.Net? [closed] asp.net asp.net

Are there any good tools for integrating GWT with ASP.Net? [closed]


I am working on a GWT project where ASP.NET is my only option on the server. I have found that, while it requires a little extra work, JavaScript Overlay types can make it easy. It doesn't matter what technology your server is using, so long as it can respond with JSON serialized data. For instance, if you have a Contact class in C#:

public class Contact {  public int Id { get; set; }  public string LastName { get; set; }  public string FirstName { get; set; }  public string Email { get; set; }}

Design your server so that it returns this serialized as JSON. I use ASP.NET MVC for this because it requires so little work. Here's a very simple example where we'll assume that Contact has a static method that, given an id, will return a Contact instance:

public class ContactController : Controller {  public ActionResult GetContact (int id)   {    return Json(Contact.GetById(id), JsonRequestBehavior.AllowGet);  }}

Now, in your GWT application, create a JavaScript overlay type for your Contact:

import com.google.gwt.core.client.JavaScriptObject;public class Contact extends JavaScriptObject {  protected Contact() { }  public final native int getContactId() /*-{ return this.Id; }-*/;  public final native String getLastName() /*-{ return this.LastName; }-*/;  public final native String getFirstName() /*-{ return this.FirstName; }-*/;  public final native String getEmail() /*-{ return this.Email; }-*/;  public static final native Contact createFromJson(String json) /*-{    return eval('(' + json + ')');  }-*/;}

Next, in your GWT project, use an HTTP request to communicate with the server:

public class ContactLoader {  private int id;   public ContactLoader(int id) {    this.id = id;  }  public void beginLoad() {    String url = "/YourService/GetContact/?id=" + id;    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);    try {      @SuppressWarnings("unused")      builder.sendRequest(null, new RequestCallback() {        @Override        public void onResponseReceived(Request req, Response resp) {          Contact contact = Contact.createFromJson(req.getText());          // do something with your contact.  In my project, I          // fire an event here for which the contact is the payload        }        @Override        public void onError(Request request, Throwable exception) {          // handle your error        }    catch (Exception exception) {      // handle your error    }  }}

It is also possible to use generic types with overlay types. For example, I never return a bare object, I always use a generic container for transport so that I can easily handle error reporting on the client side.

C#:

public class ServerResponse<T> {  public T Payload { get; set; }  public bool Success { get; set; }  public String Message { get; set; }}

Java:

public class ServerResponse<T extends JavascriptObject>   extends JavaScriptObject {  protected ServerResponse() { }  public final native T getPayload() /*-{ return this.Payload; }-*/;  public final native boolean getSuccess() /*-{ return this.Success; }-*/;  public final native String getMessage() /*-{ return this.Message; }-*/;}

This lets me return my Contact, an array of Contacts or whatever. It also helps in reducing duplication in your data loading logic.

This is an overly simple example, but hopefully it is enough to clear a path for anyone needing help with a non-Java back-end. Another resource that I found helpful that discusses overlay types is "JSON Parsing with JavaScript Overlay Types in GWT" by Matt Raible.


According to the GWT FAQ, you should be able to do this.

The heart of AJAX is making data read/write calls to a server from the JavaScript application running in the browser. GWT is "RPC agnostic" and has no particular requirements about what protocol is used to issue RPC requests, or even what language the server code is written in. Although GWT provides a library of classes that makes the RPC communications with a J2EE server extremely easy, you are not required to use them. Instead you can build custom HTTP requests to retrieve, for example, JSON or XML-formatted data.

I'm not aware of any projects to simplify the object serialization/deserialization logic though, if that was your question.

The client-side GWT code gets compiled to javascript, therefore it doesn't matter what server you use to serve it.


I've been looking for the same thing - I've found something similar to what GWT does for ASP.Net called Script#. It is written by the same guy that was mainly responsible for ASP.NET AJAX - Nikhil Kothari. The site is: http://projects.nikhilk.net/ScriptSharp

It basically takes C# code and compiles it to Javascript - the same thing that GWT does (only it does it with Java)

I honestly think that GWT can work with ASP.NET and even ASP.NET MVC - I reckon that you'd need JSON or XML serving generic handlers (.ashx) in ASP.NET for GWT to work. I havent tried it myself..

Forgive me if I've confused anyone..