PDA

View Full Version : Simple ASPX sample



alicia.marinache
30 Jul 2009, 7:08 AM
Hi all,

I am quite new to ExtJS and I need to help a co-worker with something related.
I'd like to mention from start that I didn't spend a lot of time going through all samples and demos - I googled a few of my questions and I haven't found the answer yet.

This being said - I am in dire need of an extremly simple example of how to consume data from ASP.Net. I have a simple Aspx page returning an Xml, I even tried to return a Json data structure - using Jayrock. Needless to say, my problem is on the ExtJS side.

I am not quite sure how to read the data, and I would need a complete example I could start with.

I have added bellow my ASPX page. I know there are better way to use it and I am actually willing (and more inclined) to try a WebService (WebMethods) rather than the Serializing the data accross. If someone has a complete sample using web services I would love to see it as well.

Thank you in advance for all your help!

Alicia

----------------------------

-- Person class -------------

public class Person
{
// simplified version
public Guid Id;
public string FirstName;
public string LastName;

public Person(Guid id, string firstName, stringLastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
}

-- ASPX page --------------

protectedvoid Page_Load(object sender, EventArgs e)
{
string sXml = "<Data Id=\"2160c605-2a58-45e6-a786-2b503e9e00d0\" FirstName=\"John\" LastName=\"Doe\" />";
string type = Request["type"];
if (type != null)
{
type = type.ToLower();
if (type == "xml")
{
Response.ContentType = "text/javascript";
Response.Write(sXml);
Response.End();
}
elseif (type == "json")
{
Response.ContentType = "application/x-json";
Person person = new Person(new Guid("2160c605-2a58-45e6-a786-2b503e9e00d0"), "John", "Doe");
Response.Write(Jayrock.Json.Conversion.JsonConvert.ExportToString(person));
Response.End();
}
}
}

evant
30 Jul 2009, 7:19 AM
http://extjs.com/forum/showthread.php?t=68161

andycramb
30 Jul 2009, 7:22 AM
http://extjs.com/forum/showthread.php?t=59903

alicia.marinache
7 Aug 2009, 7:47 PM
After going through all the samples (Thank you for all the help so far!) I managed something, but not all.

Here is my current situation:

If I use a normal ASPX page to return a Json structure all is well

ASPX.Net code snippet:



// Person Class
public class Person
{
public Guid Id;
public string FirstName;
public string LastName;

public Person(string id, string firstName, string lastName)
{
Id = new Guid(id);
FirstName = firstName;
LastName = lastName;
}
}

Page_Load()
{
// create a new Person
Person person = new Person("2160c605-2a58-45e6-a786-2b503e9e00d0", "Joe", "Smith");
msg = "{" + String.Format("total: {0}, rows: [{1}]",
1,
Jayrock.Json.Conversion.JsonConvert.ExportToString(person)
) + "}";

Response.ContentType = "text/javascript";
Response.Write(msg);
Response.End();
}


Ext code:




PersonDataStore = new Ext.data.Store(
{
id: 'PersonDataStore',
proxy: new Ext.data.HttpProxy(
{
url: 'http://localhost/Json_Person.aspx',
jsonData: {}
method: 'post',
headers: { 'Content-Type': 'application/json;charset=utf-8' }
}
),
reader: new Ext.data.JsonReader({
idProperty: 'id',
root: 'rows',
totalProperty: 'total',
fields: [
{ name: 'id' },
{ name: 'firstName' },
{ name: 'lastName' }
]
})
});



All is well and dandy, I can see the information being passed and my Json data looks like this:



{total: 1, rows: [{"id":"2160c605-2a58-45e6-a786-2b503e9e00d0","firstName":"Joe", "lastName":"Smith"}]}


If I try to use a Web Service instead, something goes wrong and I am not sure why...

WebService code


[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
publicobject getById()
{
Person person = new Person("2160c605-2a58-45e6-a786-2b503e9e00d0", "Joe", "Smith"); return"{" + String.Format("total: {0}, rows: [{1}]", 1, Jayrock.Json.Conversion.JsonConvert.ExportToString(person)) + "}";
}


In the Ext all I change is the url to:

url: 'http://localhost/SvcPerson.asmx/getById'


But what I get for a result in Ext is this:


{total: 1, rows: [{\"id\":\"2160c605-2a58-45e6-a786-2b503e9e00d0\",\"firstName\":\"Joe\", \"lastName\":\"Smith\"}]}



Obviously now the Json reader won't get any information...


I read about how you use the new Json {} in a previous thread between andycramb and mahroon... but I am not sure how would that line (which seems to solve all the issues) would translate in C# (the language I am using in the Web Service).

Oh, needless to say - I am using .Net 2.0 framework... but as I saw in previous samples this string wrapping situation won't dissapear if I upgrade to 3.n... it will just add the "d" wrapper :)

I feel I am so close to fix all my troubles - does anyone has any ideas for this situation?

Thanks again!

andycramb
10 Aug 2009, 7:34 AM
Where is it going wrong, the ASP.NET side?

Your method return is an object
publicobject getById()

but you return a string??
String.Format(What is the reponse in Firebug?

Do you see that JSON in the reponse?



{total: 1, rows: [{\"id\":\"2160c605-2a58-45e6-a786-2b503e9e00d0\",\"firstName\":\"Joe\", \"lastName\":\"Smith\"}]}

alicia.marinache
10 Aug 2009, 7:49 AM
Hi Andy,

First of all - Thanks! Your answers have been like a lifeline for me :)

OK, a few details:

1. I do return a string (I tried different ideas and thta's when I left the object in there).

I do have 2 methods in the Web Service:



public Person getPerson()
{
Person p = new Person();
return p;
}


This returns back a Json structure, but without the rows, total etc... so I should use a custom reader and I simply couldn't figure out how... This is the solution I would prefer, obviously :)

The second Web Service method is the one with the string:



public string getPerson_string
{
Person p = new Person();
return"{" + String.Format("total: {0}, rows: [{1}]", 1, Jayrock.Json.Conversion.JsonConvert.ExportToString(person)) + "}";

}


This one returns the following in the debugger.



{total: 1, rows: [{\"id\":\"2160c605-2a58-45e6-a786-2b503e9e00d0\",\"firstName\":\"Joe\", \"lastName\":\"Smith\"}]}


Yes, that's the JSon string I get when debugging...

I am not sure where it goes wrong, to be hoest. When I test the service I see the strings properly (although wrapped between <string> </string> tags).

At this point everything seems to almost OK with 2 issues:
* the version returning the Person object needs a custom reader and I am not sure how to build that (if you know of any samples, that would be great)...
* the version returning a string would transfor all quotes into \quote...

Thanks a lot!

andycramb
10 Aug 2009, 10:16 AM
Can you post a screenshot of the response in Firebug and add the exception handler to the store and write the error out to the console and post the error
Or better still can you put it online?

Or try this:


[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Object getPerson(){

Person p = new Person();

var data = new {id=1, rows=p, totalProperty=1, success=true};
return data;

}

alicia.marinache
10 Aug 2009, 10:29 AM
There are no errors and no exceptions. I do display the Web Service response using console.log() to see what do I get in return.

I will try to make a version that can be attached...

And - that's my problem:


var data = new {id=1, rows=p, totalProperty=1, success=true};


I cannot compile my Web Service with that line of code! I am using .Net Framework 2.0 and C# as the Service language. That line of code seems to be a Java syntax... am I missing something?

Thanks again!

Alicia

andycramb
10 Aug 2009, 10:43 AM
sorry yeah just picked up your on 2
Its one of the new features of C# 3 - anonymous types - very handy for this as you can see

OK lets see the screenshot

alicia.marinache
10 Aug 2009, 1:27 PM
And that would explain everything :)

OK, I did some more digging and - silly me - it was a line in the Custom reader that wouldn't read my info. You know how you have that d wrapping everything up in 3.X? Well... I don't need it, being in 2.X...

So what am I trying to say? That it was working all along, I was not reading properly.

And that being said - thanks again for all your help! I am a bit more knowledgeable now after reading all the posts.

I am sure I will come back for more wisdom - until then...

Thanks! and Cheers!

Alicia