public class UserController : ApiController
{
// POST /api/user
public int CreateUser(string username, string password)
{
LoginManager manager = new LoginManager();
int sdf = manager.CreateUser(new User(username, password));
return sdf;
}
}
My client (XNA project) has this RestSharp code
public void CreateUser()
{
RestRequest request = new RestRequest(Method.POST) {RequestFormat = DataFormat.Json, Resource = "user/}"};
CUser user = new CUser
{
Username = "Foo2",
Password = "BarBaz2"
};
request.AddParameter("Username", user.Username);
request.AddParameter("Password", user.Password);
client.PostAsync<int>(request, (response, handler) =>
{
switch (response.Data)
{
case 0:
//Username is taken
break;
case 1:
//Success
break;
case 2:
break;
}
});
}
Restsharp casts a Invalid cast exception and response.Data cant be evaluated. Tried changing the type but no result.
// POST /api/user
public int CreateUser(string username, string password)
How you can say that this action will be called when you do a POST?
Also note, it is better to test your action with fiddler to see whet response you are getting.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
This is not possible. You may get successfull respose(200)or error respose(4XX, 5XX) or other server respose or server not found or DNS lookup failed, etc
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Mech0z
Member
34 Points
40 Posts
Getting Invalid cast exception with MVC 4 API using Restsharp
May 27, 2012 06:41 PM|LINK
My API looks like this and returns a int "5"
public class UserController : ApiController { // POST /api/user public int CreateUser(string username, string password) { LoginManager manager = new LoginManager(); int sdf = manager.CreateUser(new User(username, password)); return sdf; } }My client (XNA project) has this RestSharp code
public void CreateUser() { RestRequest request = new RestRequest(Method.POST) {RequestFormat = DataFormat.Json, Resource = "user/}"}; CUser user = new CUser { Username = "Foo2", Password = "BarBaz2" }; request.AddParameter("Username", user.Username); request.AddParameter("Password", user.Password); client.PostAsync<int>(request, (response, handler) => { switch (response.Data) { case 0: //Username is taken break; case 1: //Success break; case 2: break; } }); }Restsharp casts a Invalid cast exception and response.Data cant be evaluated. Tried changing the type but no result.
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Getting Invalid cast exception with MVC 4 API using Restsharp
May 28, 2012 02:45 AM|LINK
at which line is it throwing the error ? may i request you to alert or log and check whats the value of in
Mech0z
Member
34 Points
40 Posts
Re: Getting Invalid cast exception with MVC 4 API using Restsharp
May 28, 2012 07:46 AM|LINK
A first chance exception of type 'System.InvalidCastException' occurred in RestSharp.WindowsPhone.dll
0
Error
When I do
client.GetAsync<int>(request, (response, handler) => { Debug.WriteLine(response.Data); Debug.WriteLine(response.ResponseStatus);The exception is as said caught in RestSharp, I only see the error in my output window
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Getting Invalid cast exception with MVC 4 API using Restsharp
May 28, 2012 02:12 PM|LINK
How you can say that this action will be called when you do a POST?
Also note, it is better to test your action with fiddler to see whet response you are getting.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Mech0z
Member
34 Points
40 Posts
Re: Getting Invalid cast exception with MVC 4 API using Restsharp
May 28, 2012 04:15 PM|LINK
I cant get fiddler to catch anything even though I used http://phone7.wordpress.com/2010/10/17/fiddler-and-wp7-emulator-working/ not even if I deploy my webserver
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Getting Invalid cast exception with MVC 4 API using Restsharp
May 28, 2012 05:13 PM|LINK
This is not possible. You may get successfull respose(200)or error respose(4XX, 5XX) or other server respose or server not found or DNS lookup failed, etc
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD