How would I handle something like the below uri using ASP.NET MVC's routing capability:
http://localhost/users/{username}/bookmarks/ - GET
http://localhost/users/{username}/bookmark/{bookmarkid} - PUT
Which lists the bookmarks for the user in {username}.
Thanks
MVC is about how the inner side of your app works. REST is about how your app "talks" with other apps. You can combine them. MVC is a design pattern for creating a separation of concerns and avoiding tightly coupled data, business, and presentation logic.
ASP.NET MVC is one such framework that provides an inherently RESTful model for building XHTML-based services.
NET Framework MVC. REST means “Representational State Transfer” and API means Application Programming Interface. Why do we use API? We use API to provide data to our application, it could be web apps, mobile apps, or any desktop app.
If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.
You can use the [AcceptVerbs] attribute on your action method
public class BookmarksController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public void Bookmarks(string user)
{
//add your bookmark
}
[AcceptVerbs(HttpVerbs.Post)]
public void Bookmarks(string user, int? id)
{
//add your bookmark
}
}
first you need to create a new route in global.aspx
routes.MapRoute("Bookmarks", "{controller}/{user}/{action}/{id}");
then add a new action
public class UsersController : Controller
{
[AcceptVerbs("Post")]
public void Bookmarks(string user, int? id)
{
//add your bookmark
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With