I am trying to get a hold of using RESTful on MVC 4 to give me an API I can use on WP/Android/Iphone, so for starters (This method will be remade later, this is just for testing purposes)
I have a WP app that communicates with the server and sends it a json object and recieves a json object using restclient
private void ButtonOptionTap(object sender, EventArgs e)
{
RestClient client = new RestClient();
client.BaseUrl = "http://localhost:21688/API/Account/Login";
RestRequest request = new RestRequest();
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
User user = new User
{
UserName = "muhcow",
Password = "123456"
};
request.AddBody(user);
client.PostAsync<LoginResult>(request, (response, ds) => {
LoginResult result = response.Data;
System.Diagnostics.Debug.WriteLine(response.Content);
});
}
And then I have my servercode which I first tried making a normal controller and then just making a controller called account and an action called login taking a httppost, and that worked fine, but from what I can read its not the way to do rest!
So I tried making a controller inheriting from apicontroller but now I got issues even getting routed to the Post action in the LoginController
Mech0z
Member
34 Points
40 Posts
Cant get my routing to work
Apr 27, 2012 09:40 AM|LINK
I am trying to get a hold of using RESTful on MVC 4 to give me an API I can use on WP/Android/Iphone, so for starters (This method will be remade later, this is just for testing purposes)
I have a WP app that communicates with the server and sends it a json object and recieves a json object using restclient
private void ButtonOptionTap(object sender, EventArgs e) { RestClient client = new RestClient(); client.BaseUrl = "http://localhost:21688/API/Account/Login"; RestRequest request = new RestRequest(); request.Method = Method.POST; request.RequestFormat = DataFormat.Json; User user = new User { UserName = "muhcow", Password = "123456" }; request.AddBody(user); client.PostAsync<LoginResult>(request, (response, ds) => { LoginResult result = response.Data; System.Diagnostics.Debug.WriteLine(response.Content); }); }And then I have my servercode which I first tried making a normal controller and then just making a controller called account and an action called login taking a httppost, and that worked fine, but from what I can read its not the way to do rest!
So I tried making a controller inheriting from apicontroller but now I got issues even getting routed to the Post action in the LoginController
So post is in Domain\API\Login\LoginController
And the post action is like so
but with the current code that is not hit (Checking with breakpoints)
I havent changed the MapRoute yet so its just
DarrellNorto...
All-Star
86713 Points
9638 Posts
Moderator
MVP
Re: Cant get my routing to work
Apr 27, 2012 10:16 AM|LINK
You need to use MapHttpRoute not MapRoute for WebApi to work:
routes.MapHttpRoute("Default API Route", "api/1.0/{controller}/{id}", new { id = RouteParameter.Optional });Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Mech0z
Member
34 Points
40 Posts
Re: Cant get my routing to work
Apr 27, 2012 10:39 AM|LINK
So my APIAreaRegistration needs to changed?
using System.Web.Mvc; namespace WebSite.Areas.API { public class APIAreaRegistration : AreaRegistration { public override string AreaName { get { return "API"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapHttpRoute( "API_default", "API/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } }to something like this?
public static void RegisterRoutes(RouteCollection routes) { var Route1 = routes.MapHttpRoute ( name: "ClientApi", routeTemplate: "client/{controller}/{param1}" ); Route1.DataTokens["Namespaces"] = new string[] {"Client"}; var Route2 = routes.MapHttpRoute ( name: "OrderApi", routeTemplate: "order/{controller}/{param1}" ); Route2.DataTokens["Namespaces"] = new string[] { "Order" }; }If yes, how do I do that for areas or should I change the Global.asax?