I’ve got a URL routing issue in my application. I’ve tried to tackle the issue, but haven’t really made any headway.
So, I’m making the following AJAX request client-side:
$.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);
}
});
Here’s the network:

Here’s the server error:

Here’s the Controller’s methods for GET and 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);
}
and finally, here are my routes:
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}
);
As best as I can tell — I am getting my URL routed to my ‘Get’ action, but I am intended it to be routed to the ‘GetPlaylistsByUserId’ action. I believe this to be happening because the server error states that it is looking for a paramater ‘id’ for method ‘Get.’
I’m not sure why any of this would be happening, though, as I seem to quite clearly have my other action mapped…?
Compare the route to the request – you’re requesting an action name, which doesn’t match the route. As it stands, your routing expects your ajax request to go to
.../Playlist/SomeGuidas opposed toPlaylist/GetPlaylistsByUserId?userId=SomeGuid.If you want to route all requests to your
Playlistcontroller to the GetPlaylistsByUserId action if no action is specified, the route you want is:Note the omission of userId – this is passed as a querystring and doesn’t need to be included in your route. MVC will bind this automatically.
As it stands, however, you are requesting an action name, so the following route will pick this up:
You could also use
{controller}/{action}but I am guessing you don’t want all controllers to default to an action calledGetPlaylistsByUserId.