Recommended API design with ASP.NET MVC3?

You need to also define proper routes in global. Asax or use the default route which looks like {controller}/{action}/{id} where controller is defaulted to "Home", action is defaulted to "Index" and id is optional.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I'm working with ASP. NET MVC 3. I'm kind of new to it.

I think I'm starting to get the hang of it. But there is something that I'm trying to do, that I think makes sense, but maybe I'm wrong. I'm trying to create an API around Order objects in my database.

In order to get all of the orders in the system, I was going to expose an API that looks like the following: /orders/ In cases where I wanted to get a specific Order, I would simply append an ID. In other words, the URL would look like this: /orders/12345 In an effort to accomplish this, I created the following controller: public class OrdersController : Controller { // GET: /Orders/ AcceptVerbs(HttpVerbs. Get) public ActionResult Index() { string result = "list of orders"; return Json(result, JsonRequestBehavior.

AllowGet); } // // GET: /Orders/{orderID} public ActionResult Index(int id) { string result = "order:" + id; return Json(result, JsonRequestBehavior. AllowGet); } } In my AreaRegistration class, I have the following: public override void RegisterArea(AreaRegistrationContext context) { context. MapRoute( "OrderList", "{controller}/{action}", new { action = "Index", controller="Orders" } ); context.

MapRoute( "Order", "{controller}/{action}/{id}", new { action = "Index", controller = "Orders" } ); } When I attempted to access "/orders/", via the browser address bar, I get the JSON like I would expect. However, if I attempt to access "/orders/12345", I receive a 404. What am I missing?

Thank you asp. Net-mvc-3 link|improve this question edited Dec 1 '11 at 21:57 asked Dec 1 '11 at 20:38Mono Developer1895 65% accept rate.

– 32bitkid Dec 1 '11 at 20:41 I haven't done anything to it. Because this controller is in an Area, I wasn't sure where to setup the routing. – Mono Developer Dec 1 '11 at 20:54.

You need to also define proper routes in global. Asax or use the default route which looks like {controller}/{action}/{id} where controller is defaulted to "Home", action is defaulted to "Index" and id is optional. So /orders works because you have defined controller (orders), default action (Index) and missing id (which doesn't matter as it is optional) But when you try /orders/12345 then you have defined controller (orders), action (12345) and missing id So to make this work with only the default route the request should be /orders/index/12345 edit: for registering area routes you should use AreaRegistration class.

I concur 100% here +1, adding only that if you want a separate route simply add it above the default route and specify /orders/{id} instead. – Adam Tuliper Dec 1 '11 at 21:05 this is what I was hinting at. +1 :) – 32bitkid Dec 1 '11 at 21:18 I added the following to my AreaRegistration and I still get a 404 " context.

MapRoute( "Order", "{controller}/{action}/{id}", new { action = "Index", controller="Orders" } ); – Mono Developer Dec 1 '11 at 21:25 @MonoDeveloper but the second argument isn't what your desired url looks like... read this: asp.net/mvc/tutorials/controllers-and-ro... – 32bitkid Dec 1 '11 at 21:33 I'm sorry, I'm not familiar with what you're saying. Are you saying I can NOT accomplish what I'm trying? I've included my AreaRegisration code in the question text above.

– Mono Developer Dec 1 '11 at 21:57.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions