4

我的应用程序中存在 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”。

不过,我不确定为什么会发生这种情况,因为我似乎很清楚地映射了我的其他动作......?

4

4 回答 4

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.

于 2013-01-25T18:49:34.107 回答
2
routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

我不确定这是否可能,但有没有可能是因为你{action}的 Url 模式中没有?

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{action}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);
于 2013-01-25T18:51:37.987 回答
2

您的问题是请求网址看起来像

Playlist/GetPlaylistsByUserId?userid=51d77etc...

并且您的路由设置为:

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

他们不匹配。

要使用此路由,URL 应如下所示:

Playlist?userid=51d77etc...

或者你的 url 应该保持不变并且路由映射应该是

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{action}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);
于 2013-01-25T18:55:41.287 回答
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") }
);
于 2013-01-25T19:04:18.640 回答