# 1.2.2.Route

## 1. Convention-based route

* 客製化url: /Movies/released/2015/4
  * 1.在App\_start/RouteConfig.cs中加入以下code, 他將會去呼叫MoviesController.cs中的ByReleaseDate

    ```
      routes.MapRoute(
          "MovieByReleaseDate",
          "movies/released/{year}/{month}",
          new { controller = "Movies", action = "ByReleaseDate" });
    ```
  * 2.在MoviesController.cs中加入以下code
    * mvcaction4 + tab 可快速產生 public ActionResult

      ```
      public ActionResult ByReleaseDate(int year, int month)
      {
        return Content(year + "/" + month);
      }
      ```
  * 3.在瀏覽器中輸入: <http://localhost:3968/Movies/released/2015/4>   &#x20;

## 2. Attribute route

* To avoid RouteConfig.cs and controller were not  consistent
* 1.修改route: RouteConfig.cs

  ```
            routes.MapMvcAttributeRoutes();
  ```
* 2.修改controller: MoviesController.cs
  * Constraints
    * min
    * max
    * minlength
    * maxlength
    * int
    * float
    * guid   &#x20;

```
            [Route("movies/released/{year}/{month:regex(\\d{4}):range(1, 12)}")]
             public ActionResult ByReleaseDate(int year, int month)
             {
                return Content(year + "/" + month);
             }
```
