Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

How to configure routing in asp.net web api, to that I can code for the following actions in my ApiController inherited class?

|======================================================================================|
|Http Verb| Path             | Action   | Used for                                     |
|======================================================================================|
| GET     | /photos          |  index   | display a list of all photos                 |
| GET     | /photos/new      |  new     | return an HTML form for creating a new photo |
| POST    | /photos/         |  create  | create a new photo                           |
| GET     | /photos/:id      |  show    | display a specific photo                     |
| GET     | /photos/:id/edit |  edit    | return an HTML form for editing a photo      |
| PUT     | /photos/:id      |  update  | update a specific photo                      |
| DELETE  | /photos/:id      |  destroy | delete a specific photo                      |

share|improve this question
    
Are you trying to return a browser-friendly HTML page, an API-style data response, or some of both? It sounds to me like you're trying to return HTML, in which case you don't want to use WebAPI. You just want to use MVC. – Brian Reischl Jun 28 '13 at 14:53
    
Hi Brian, I am returning json, working on a Single page application using Knockout and SammyJS – Jas Jun 28 '13 at 15:09

2 Answers 2

I found http://restfulrouting.com/ to be very useful. It allows nested resources too.

share|improve this answer

Below is my own working solution for this:
//Route configuration:

//GET     | /photos          |  index   | display a list of all photos    
config.Routes.MapHttpRoute(
    name: "DefaultIndex",
    routeTemplate: "api/{controller}",
    defaults: new {action = "Index"},
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

//POST    | /photos/         |  create  | create a new photo
config.Routes.MapHttpRoute(
    name: "DefaultCreate",
    routeTemplate: "api/{controller}",
    defaults: new { action = "Create" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);

//GET     | /photos/new      |  new     | return an HTML form for creating a new photo |
config.Routes.MapHttpRoute(
    name: "DefaultNew",
    routeTemplate: "api/{controller}/new",
    defaults: new { action = "New" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

//GET     | /photos/:id      |  show    | display a specific photo    
config.Routes.MapHttpRoute(
    name: "DefaultShow",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "Show" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

//PUT     | /photos/:id      |  update  | update a specific photo   
config.Routes.MapHttpRoute(
    name: "DefaultUpdate",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "Update" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
);

//DELETE  | /photos/:id      |  destroy | delete a specific photo 
config.Routes.MapHttpRoute(
    name: "DefaultDestroy",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "Destroy" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Delete) }
);

config.Routes.MapHttpRoute(
    name: "DefaultEdit",
    routeTemplate: "api/{controller}/{id}/edit",
    defaults: new { action = "Edit" },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

//ApiController Actions

// GET api/photo
[ActionName("Index")]
public string Get()
{
    return "Index Action called";
}


// GET api/photos/5
[ActionName("Show")]
public string Get(int id)
{
    return "Show action called"
}

// GET api/photos/5/edit
[HttpGet]
public string Edit(int id)
{
    return "Edit action called";
}

// POST api/photos
[ActionName("Create")]
public void Post([FromBody]string value)
{

}

// GET api/photos/new
[HttpGet]
public string New()
{
    return "New called";
}

// PUT api/photos/5
[ActionName("Update")]
public void Put(int id, [FromBody]string value)
{
}

// DELETE api/photos/5
[ActionName("Destroy")]
public void Delete(int id)
{
}
share|improve this answer
1  
you have it all wrong, this is not restful. Restful doesn't say you have CRUD names in the url. That's totally not how REST is done. You use HTTP verbs in the HttpReques that determine whether you are doing an update, create, delte (so GET, POST, ETC.) determines the action, you shouldn't be having your action in the url. You map the RESTful url to the action method in your controller. In controller is where you will have methods Create Delete, Update. Not methods called GEt. You have it backwards. – MSSucks Sep 20 '13 at 19:35
    
Hmm, I going to assume you haven't worked with WebApi? In which case his example is perfectly fine. The reason he writes GET, is because the framework picks it up as a HttpGet for that given function in that controller. – Casper Sloth Paulsen Jul 19 '14 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.