我的应用程序中存在 URL 路由问题。我试图解决这个问题,但还没有真正取得任何进展。
因此,我在客户端发出以下 AJAX 请求:
$.ajax({
type: 'GET',
url: programState.getBaseUrl() + 'Playlist/GetPlaylistsByUserId',
dataType: 'json',
data: {
userId: user.get('id')
},
success: function (data) {
console.log("JSON data:", data);
},
error: function(error) {
console.error(error);
}
});
这是网络:
这是服务器错误:
这是控制器的 GET 和 GetPlaylistsByUserId 方法:
[HttpGet]
public ActionResult Get(Guid id)
{
Playlist playlist = PlaylistDao.GetById(id);
return new JsonDataContractActionResult(playlist);
}
[HttpGet, ActionName("GetPlaylistsByUserId")]
public ActionResult GetPlaylistsByUserId(Guid userId)
{
IList<Playlist> playlists = PlaylistDao.GetByUserId(userId);
return new JsonDataContractActionResult(playlists);
}
最后,这是我的路线:
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"post-Playlist",
"{controller}",
new { controller = "Playlist", action = "create" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"get-Playlist",
"{controller}/{action}/{id}",
new { controller = "Playlist", action = "get" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"put-Playlist",
"{controller}",
new { controller = "Playlist", action = "update" },
new { httpMethod = new HttpMethodConstraint("PUT") }
);
routes.MapRoute(
"delete-Playlist",
"{controller}/{id}",
new { controller = "Playlist", action = "delete" },
new { httpMethod = new HttpMethodConstraint("DELETE") }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
尽我所能 - 我将我的 URL 路由到我的“获取”操作,但我打算将它路由到“GetPlaylistsByUserId”操作。我相信这会发生,因为服务器错误表明它正在为方法“Get”寻找参数“id”。
不过,我不确定为什么会发生这种情况,因为我似乎很清楚地映射了我的其他动作......?