问题描述
我有一个动作方法,它返回一个视图并将视图模型传递给视图, 在我的应用程序的另一部分通过输入参数调用此方法后 url 看起来像这样 (localhost:51999:\Home\Onlineplayer{guid})
public async Task<IActionResult> OnlinePlayer(Guid id)
{
if (id != Guid.Empty)
{
var requestedFeed = await _FeedService.FindOneAsync(id);
var FeedOwnerName = requestedFeed.OwnerName;
ISpecification<Feed> spec = new FeedWithNotEndedStateSpec()
.And(new FeedWithVerifiedStateSpec())
.And(new FeedWithSpecificOwnerNameSpec(FeedOwnerName));
onlinePlayerVm.LatestMusics = await _FeedService.GetListBySpecAsync<FeedPlayerDTOVm>
(new PaginationFilter(),spec);
return View(onlinePlayerVm);
}
onlinePlayerVm.LatestMusics =
await _FeedService.GetListAsync<FeedPlayerDTOVm>(new PaginationFilter(1,20));
return View(onlinePlayerVm);
}
OnlinePlayer.cshtml 有一个 'document.Ready()' jquery 函数来调用页面加载中的一些函数:
<script src="~/js/Custom/AudioPlayer.js" asp-append-version="true"></script>
<script>
$(document).ready(function () {
_latestMusicCurrentPage = 1;
_topMusicCurrentPage = 1;
_popMusicCurrentPage = 1;
//GrablatestMusics();
if (@User.Identity.IsAuthenticated.ToString().ToLower()) {
SaveLocalPlaylistToDatabaseAfterLogin();
//GrabUserPlaylists();
}
//LoadLocalPlaylists();
var item = $("#playlistContainer").find("p[data-id=1]");
PlayThisFromList(this,item,false);
$(".RemoveFromList").click(function (e) {
var dataId = $(this).attr('data-id');
$(this).closest("p[data-id='" + dataId + "']").remove();
e.stopPropagation();
});
$(".DownloadThisMusic").click(function (e) {
var dataId = $(this).attr('data-id');
PNotifyToasterWithParam("error","پیاده سازی نشده است")
e.stopPropagation();
});
});
function GrablatestMusics() {
debugger
$.post('LatestMusic',{ pageIndex: _topMusicCurrentPage },function (data) {
$("#latestSongsContainer").append(data);
}).done(function () {
SetColumntResizingAfterPlayerHidden();
});;
}
</script>
问题 当 GrablatestMusics() 函数是注释时,页面及其脚本工作正常,但在取消注释此函数并在 url 查询字符串中传递 Id 后,页面将崩溃并且文档准备好一遍又一遍地执行并发送对控制器中 OnlinePlayer(Guid id) 操作方法的无限请求。我该如何解决这个问题!?
解决方法
在GrabLatestMusics方法,你应该使用控制器和动作的详细地址。 (如果需要,也可以使用区域)
function GrabLatestMusics() {
$.post('/YourArea/YourController/LatestMusic',...