我的应用程序中存在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的Controller方法:
[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}
);
尽我所知 - 我将我的网址路由到我的“获取”操作,但我打算将其路由到“GetPlaylistsByUserId”操作。我相信这会发生,因为服务器错误表明它正在寻找方法'Get'的参数'id'。
我不确定为什么会发生这种情况,因为我似乎非常明确地将我的其他行动映射到了......?
答案 0 :(得分:2)
将路线与请求进行比较 - 您要求的行动名称与路线不匹配。目前,您的路由期望您的ajax请求转到.../Playlist/SomeGuid
,而不是Playlist/GetPlaylistsByUserId?userId=SomeGuid
。
如果您想要将所有请求路由到Playlist
控制器到GetPlaylistsByUserId操作,如果未指定任何操作,则您需要的路径是:
routes.MapRoute(
"getPlaylistsByUserId",
"Playlist",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
请注意省略userId - 这是作为查询字符串传递的,不需要包含在您的路由中。 MVC将自动绑定它。
然而,按照目前的情况,你是请求一个动作名称,所以以下路线会选择这个:
routes.MapRoute(
"getPlaylistsByUserId",
"Playlist/{action}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
您也可以使用{controller}/{action}
,但我猜您不希望所有控制器默认为名为GetPlaylistsByUserId
的操作。
答案 1 :(得分:2)
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
我不确定这是不是可以,但是有没有机会,因为你的Url模式中没有{action}
?
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{action}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
答案 2 :(得分:2)
您的问题是请求网址看起来像
Playlist/GetPlaylistsByUserId?userid=51d77etc...
,您的路由设置为:
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
他们不匹配。
对于要使用的路径,网址应如下所示:
Playlist?userid=51d77etc...
或您的网址应保持不变,路线映射应为
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{action}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
答案 3 :(得分:1)
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
应改为
routes.MapRoute(
"getPlaylistsByUserId",
"{controller}/GetPlaylistsByUserId/{userId}",
new { controller = "Playlist", action = "GetPlaylistsByUserId" },
new { httpMethod = new HttpMethodConstraint("GET") }
);