2

如何在 asp.net web api 中配置路由,以便我可以在我的 ApiController 继承类中为以下操作编写代码?

|=================================================== ======================================|
| Http 动词| 路径             | 行动   | 用于                                     |
|=================================================== ======================================|
| 获取 | /照片 | 索引 | 显示所有照片列表 |
| 获取 | /照片/新 | 新 | 返回用于创建新照片的 HTML 表单 |
| 发布 | /照片/ | 创建 | 创建一张新照片 |
| 获取 | /照片/:id | 显示 | 显示特定照片 |
| 获取 | /照片/:id/编辑 | 编辑 | 返回用于编辑照片的 HTML 表单 |
| 放 | /照片/:id | 更新 | 更新特定照片 |
| 删除 | /照片/:id | 销毁| 删除特定照片 |

4

2 回答 2

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)
{
}
于 2013-06-28T15:26:17.923 回答
1

我发现http://restfulrouting.com/非常有用。它也允许嵌套资源。

于 2013-09-11T02:02:59.040 回答