Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A RESTful library for ASP.NET MVC

License

NotificationsYou must be signed in to change notification settings

bbyars/RestMvc

Repository files navigation

RestMvc is a simple library for building RESTful services in ASP.NET MVC.It's primary purpose is to provide routing and content negotiation (conneg).The routing differs from other RESTful routing libraries, like SimplyRestful,in that the route is defined alongside the action that receives the route.It was largely inspired by the Ruby framework Sinatra (http://www.sinatrarb.com/).

Examples

public class OrdersController : Controller{    [Get("/orders")]    public ActionResult Index() { ... }    [Post("/orders"]    public ActionResult Create() { ... }    [Get("/orders/{id}.format", "/orders/{id}")]    public ActionResult Show(string id) { ... }    [Put("/orders/{id}")]    public ActionResult Edit(string id) { ... }    [Delete("/orders/{id}")]    public ActionResult Destroy(string id) { ... }}// In Global.asax.csRouteTable.Routes.Map<OrdersController>();// or RouteTable.Routes.MapAssembly(Assembly.GetExecutingAssembly());

The code above will do the following:

  • Create the routes defined by the HTTP methods and URI templates in the attributes.Even though System.Web.Routing does not allow you to prefix URI templates with either/ or ~/, I find allowing those prefixes can enhance readability, and thus they are allowed.
  • Route HEAD and OPTIONS methods for the two URI templates ("orders" and "orders/{id}")to a method capable of handling those methods intelligently.
  • Route PUT and DELETE for /orders, and POST for /orders/{id}, to a methodthat knows to return a 405 HTTP status code (Method Not Supported) with an appropriateAllow header. This method and the ones that handle HEAD and OPTIONS are defined as virtualon RestfulController. If you want them mapped by RestMvc, but want to customize them(e.g., adding a body on OPTIONS), you can subclass RestfulController and override theappropriate methods.
  • Add routes for tunnelling PUT and DELETE through POST for HTML browser support.Creating a form with a hidden field called _method set to either PUT or DELETEwill route to either Edit or Destroy.
  • Notice the optional format parameter on the GET method actions (Show uses it;Index does not). Routes with an extension are routed such that the extensiongets passed as the format parameter, if the resource supports multiple representations(e.g. /orders/1.xml routes to Show with a format of xml). The ordering of the URI templatesin the Get attribute is important. Had I reversed the order, /orders/1.xml would havematched with an id of "1.xml" and an empty format

The last point is a convenient way to handle multiple formats for a resource. Sinceit's in the URL, it can be bookmarked and emailed, with the same representationregardless of the HTTP headers. Even if content negotiation is used, it allowsyou to bypass the standard negotiation process. RestMvc does not automaticallyprovide these routes for you - notice that two URIs are specified on the Show method.

Content negotiation is provided as a decorator to the standard RouteHandler.One of the problems I've seen with some other RESTful routing libraries is thatthey define the IRouteHandler internally, which removes your ability to addany custom hooks into the routing process. My hope is that providing thefunctionality as a decorator allows for more flexibility.

// In Global.asax.csvar map = new MediaTypeFormatMap();map.Add(MediaType.Html, "html");map.Add(MediaType.Xhtml, "html");map.Add(MediaType.Xml, xml");var connegRouter = new ContentNegotiationRouteProxy(new MvcRouteHandler(), map);RouteTable.Routes.Map<OrdersController>(connegRouter);// or RouteTable.Routes.MapAssembly(Assembly.GetExecutingAssembly(), connedRouter);

In the absence of a route URI template specifying the format explicitly,the connegDecorator will examine the Accept request header and pick thefirst media type supported in the map. Wildcard mathes are supported(e.g. text/* matches text/html).

The content negotiation is quite simple at the moment. The q parameter inthe Accept header is completely ignored. By default, it tries to abide bythe Accept header prioritization inferred from the order of the MIME typesin the header. However, you can change it to allow the server ordering,as defined by the order MIME types are added to the MediaTypeFormatMap,to take priority. This was added to work around what I consider to be a bugin Google Chrome - despite being unable to natively render XML, it prioritizesXML over HTML in its Accept header.

Building RestMVC

Hopefully, build.bat should do the trick. RestMVC uses CM.NET,a build library I've developed hosted athttp://github.com/bbyars/CM.NET.The output should be placed in the build directory.

The build will install the RestMvc.Example project as a virtual directoryunder the Default Web Site and run some functional tests against it.I believe the functional tests will only work on IIS 7 - I thinkwe would need to add a wildcard script map accepting all HTTP verbson previous versions of IIS.

build.bat /t:Coverage should spit out test coverage in the build directory.

  1. Contributing

Patches and suggestions are always welcome. You can reach me atbrandon.byars@gmail.com.Feel free to fork the repository athttp://github.com/bbyars/RestMvc.

About

A RESTful library for ASP.NET MVC

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp