2

如何在asp.net web api中配置路由,以便我可以在我的ApiController繼承類中爲以下操作編碼?如何在Asp.net MVC Web Api中創建Ror樣式Restful路由

 
|======================================================================================| 
|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      | 

+0

您是否試圖返回一個適合瀏覽器的HTML頁面,API風格的數據響應或其中的一部分?這聽起來像你想要返回HTML,在這種情況下,你不想使用WebAPI。你只是想使用MVC。 –

+0

嗨,布賴恩,我回來了json,在單頁面應用程序中使用Knockout和SammyJS – Jas

回答

1

下面是本我自己工作的解決方案:
//路由配置:

//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操作

// 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) 
{ 
} 
+1

工作,你有一切都錯了,這不安寧。 Restful並沒有說你在URL中有CRUD名字。這完全不是REST如何完成的。你可以在HttpReques中使用HTTP動詞來決定你是否正在進行更新,創建,刪除(所以GET,POST等)決定了這個動作,你不應該在URL中進行動作。將RESTful url映射到控制器中的action方法。在控制器中,您將擁有創建刪除,更新的方法。不是稱爲GEt的方法。你有它倒退。 – PositiveGuy

+1

嗯,我想假設你沒有使用過WebApi?在這種情況下,他的例子非常好。他寫GET的原因是,框架將其作爲該控制器中給定函數的HttpGet。 –