Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

1. Getting Started

mccalltd edited this page · 15 revisions

The simplest way: If you’re using NuGet, then issue the following command:


    Install-Package AttributeRouting

The other way: Add a reference in your project to AttributeRouting.dll. The downloadable binaries will kept up to date with what goes into NuGet.

Quickstart

Use RouteAttributes to decorate actions in your controllers. There is a specific RouteAtribute for each of the four HTTP verbs: GET, PUT, POST, and DELETE.

Below is an example of defining routes for a resource in a RESTful style. Note that all four verbs are represented.

  public class ResourcesController : ControllerBase
  {
      [GET("Resources")]
      public ActionResult Index()
      {
          return View();
      }

      [GET("Resources/New")]
      public ActionResult New()
      {
          return View();
      }

      [POST("Resources")]
      public ActionResult Create()
      {
          return RedirectToAction("Index");
      }

      [GET("Resources/{id}")]
      public ActionResult Show(int id)
      {
          return View();
      }

      [GET("Resources/{id}/Edit")]
      public ActionResult Edit(int id)
      {
          return View();
      }

      [PUT("Resources/{id}")]
      public ActionResult Update(int id)
      {
          return RedirectToAction("Show");
      }

      [GET("Resources/{id}/Delete")]
      public ActionResult Delete(int id)
      {
          return View();
      }

      [DELETE("Resources/{id}")]
      public ActionResult Destroy(int id)
      {
          return RedirectToAction("Index");
      }
  }

Once you have added the RouteAttributes to your action methods, you will need to have the AttributeRouting library find them and create corresponding routes in the route table. Simply modify the route registration code in Global.asax. NOTE: If you installed via nuget, you will have a file in the App_Start folder that will call this registration hook automagically.

  public static void RegisterRoutes(RouteCollection routes)
  {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapAttributeRoutes();
  }

When you’re done, hit F5. That’s it.

Something went wrong with that request. Please try again.