MVC 4 - 路由变成查询字符串

时间:2014-09-18 01:03:46

标签: asp.net-mvc asp.net-mvc-routing query-string

我下载了Chinook示例数据库,我正在尝试构建一个简单的应用程序来自学MVC 4和EF。 Chinook基本上是每一张专辑中的一群艺术家,他们的专辑和曲目。我想创建一个类似REST的URL结构,用于显示每个艺术家,他们的专辑和曲目。这就是我想象的:

Chinook/Artist                              <- List of artists
Chinook/Artist/{id}                         <- Artist details
Chinook/Artist/{id}/Album                   <- List of albums for artist
Chinook/Artist/{id}/Album/{id}              <- Details for an album
Chinook/Artist/{id}/Album/{id}/Track        <- Track listing for album
Chinook/Artist/{id}/Album/{id}/Track/{id}   <- Track details

这是我的Route.config的样子:

 routes.MapRoute(
    name: "chinook_artist",
    url: "Chinook/Artist/{id}/{action}",
    defaults: new { controller = "Artist", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "chinook_album",
    url: "Chinook/Artist/{ArtistId}/Album/{id}/{action}",
    defaults: new { controller = "Album", action = "Index", id = UrlParameter.Optional, ArtistId = UrlParameter.Optional }
);

routes.MapRoute(
    name: "chinook_track",
    url: "Chinook/Artist/{ArtistId}/Album/{AlbumId}/Track/{id}/{action}",
    defaults: new { controller = "Track", action = "Index", id = UrlParameter.Optional, ArtistId = UrlParameter.Optional, AlbumId = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

我的想法是对所有三种类型的对象执行EditDeleteAdd操作,并Index作为列表。

这很好用,但这就是我的网址:

http://localhost:57264/Album/?ArtistId=8&AlbumId=10&TrackId=70

在所有情况下,我都在使用Html.ActionLink控制器,操作等。在视图中生成链接。但我不能让我的网址以“Chinook”开头,而是“干净”,而不是使用查询字符串,如下所示:

http://localhost:57264/Chinook/Artist/8/Album/10/Track/70

所以我不确定我在这里做错了什么。我是否反对MVC应该如何运作?我的路线定义不正确吗?

1 个答案:

答案 0 :(得分:1)

查看最后2条路线

Chinook/Artist/{id}/Album/{id}/Track
Chinook/Artist/{id}/Album/{id}/Track/{id}

您的路线定义需要

//Chinook/Artist/{artistid}/Album/{albumid}/Track
routes.MapRoute(
  name: "chinook_track",
  url: "Chinook/Artist/{ArtistId}/Album/{AlbumId}/Track",
  defaults: new { controller = "Track", action = "Index" }
);

//Chinook/Artist/{artistid}/Album/{albumid}/Track/{id}
routes.MapRoute(
  name: "chinook_trackdetails",
  url: "Chinook/Artist/{ArtistId}/Album/{AlbumId}/Track/{id}",
  defaults: new { controller = "Track", action = "Details", id = UrlParameter.Optional }
);

和您的控制器

public class TrackController : Controller
{
  public ActionResult Index(int ArtistId, int AlbumId)
  {
    ....
  }
  public ActionResult Details(int ArtistId, int AlbumId, int id)
  {
    ....
  }
}

并在视图中,生成链接为

@Html.ActionLink("Index", "Index", "Track", new { ArtistId = 5, AlbumId  = 3 }, null)
@Html.ActionLink("Details", "Details", "Track", new { ArtistId = 5, AlbumId = 3, id = 7 }, null)

然后,如果您想要一个以类似格式编辑轨道的路线

//Chinook/Artist/{artistid}/Album/{albumid}/Track/{id}/Edit
routes.MapRoute(
  name: "chinook_trackedit",
  url: "Chinook/Artist/{ArtistId}/Album/{AlbumId}/Track/{id}/Edit",
  defaults: new { controller = "Track", action = "Edit"}
);

public ActionResult Edit(int ArtistId, int AlbumId, int id)
{
  ....
}

@Html.ActionLink("Edit", "Edit", "Track", new { ArtistId = 5, AlbumId = 3, id = 7 }, null)