重定向到URL的最佳方法(如果已更改)

时间:2018-09-13 14:21:35

标签: c# asp.net-mvc

我有两个选项卡,在我的控制器中,我写了以下内容:

[HttpGet]
[Route("ViewSongAmendments")]
[AuthorizeTenancy(Roles = "super,administrator")]
public async Task<ActionResult> ViewSongAmendments(string songCode, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                        string sortBy = "Membership", bool sortAsc = true)
{
    // AJAX?
    if (!Request.IsAjaxRequest())
    {
        return RedirectToAction("ViewSongAmendments", "SongsManagement");
    }

    return await GetWorkAndAmendmentDetails(songCode, page, take, sortBy, sortAsc);

}

[HttpGet]
[Route("UpdatedSongAmendments")]
[AuthorizeTenancy(Roles = "super,administrator")]
public async Task<ActionResult> UpdatedSongAmendments(string songCode, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                        string sortBy = "Membership", bool sortAsc = true)
{
    // AJAX?
    if (!Request.IsAjaxRequest())
    {
        return RedirectToAction("UpdatedSongAmendments", "SongsManagement");
    }

    return await GetWorkAndAmendmentDetails(songCode, page, take, sortBy, sortAsc);
}

但是这就是我的Controller的样子,有没有更好的方法可以用来进行重定向。和往常一样,我使用:

// AJAX?
if (!Request.IsAjaxRequest())
{
    return View(model);
}
return PartialView("_ViewAmendmentsWriters", model);

控制器

我添加了控制器,因为如果它不是Ajax请求,我想返回模型。但是我不确定如何实现这一目标。

[HttpGet]
    [Route("ViewSongAmendments")]
    [AuthorizeTenancy(Roles = "super,administrator")]
    public async Task<ActionResult> ViewSongAmendments(string songCode, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                            string sortBy = "Membership", bool sortAsc = true)
    {
        return await GetWorkAndAmendmentDetails(songCode, page, take, sortBy, sortAsc);
    }

    [HttpGet]
    [Route("UpdatedSongAmendments")]
    [AuthorizeTenancy(Roles = "super,administrator")]
    public async Task<ActionResult> UpdatedSongAmendments(string songCode, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                            string sortBy = "Membership", bool sortAsc = true)
    {
        return await GetWorkAndAmendmentDetails(songCode, page, take, sortBy, sortAsc);
    }

    /// <summary>
    /// Gets the work and amendment details.
    /// </summary>
    /// <param name="songCode">The song code.</param>
    /// <param name="page">The page.</param>
    /// <param name="take">The take.</param>
    /// <param name="sortBy">The sort by.</param>
    /// <param name="sortAsc">if set to <c>true</c> [sort asc].</param>
    /// <returns></returns>
    private async Task<ActionResult> GetWorkAndAmendmentDetails(string songCode, int page, int take, string sortBy, bool sortAsc)
    {
        var workAndActiveAmendments = await _artistAccountService.GetWorkAndActiveAmendmentGraphAsync(songCode, includeRecordingArtifact: true, includeWorkSplitsAndWriters: true);
        var workAndActiveAmendmentsViewModel = MapWorkAmendmentToSongAmendmentsComparisonModel(workAndActiveAmendments);

        workAndActiveAmendmentsViewModel.PagingInfo = new PagingModel
        {
            Page = page,
            Take = take,
            SortAscending = sortAsc,
            SortPropertyName = sortBy,
            PipedFilter = string.Empty
        };
        return View(workAndActiveAmendmentsViewModel);
    }

    /// <summary>
    /// Maps the work amendments to work view model.
    /// </summary>
    /// <param name="viewModel">The view model.</param>
    /// <param name="domainAccountModel">The domain account model.</param>
    private static ViewModels.SongAmendmentsComparisonModel MapWorkAmendmentToSongAmendmentsComparisonModel(WorkGraph.WorkAndAmendment workAndAmendment)
    {
        var viewModel = new ViewModels.SongAmendmentsComparisonModel
        {
            OriginalWork = new WorkAmedmentsComparisonModel
            {
                // meta
                ID = workAndAmendment.Original.ID,
                RowVersion = workAndAmendment.Original.RowVersion,
                IsDeleted = workAndAmendment.Original.IsDeleted,
                IsNew = workAndAmendment.Original.IsNew,

                // data
                Title = workAndAmendment.Original.Title,
                DurationMins = workAndAmendment.Original.DurationMins,
                DurationSecs = workAndAmendment.Original.DurationSecs,
                Iswc = workAndAmendment.Original.Iswc,
                UniqueCode = workAndAmendment.Original.UniqueCode,
                Version = workAndAmendment.Original.Version,
                IsRemix = workAndAmendment.Original.IsRemix,
                ContainsSample = workAndAmendment.Original.ContainsSamples,

                // nav items
                AlternativeTitles = workAndAmendment.Original.AlternativeTitles != null
                    ? workAndAmendment.Original.AlternativeTitles
                        .Select(x => new WorkAmendmentsAlternativeTitle
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            Title = x.Title
                        })
                        .ToList()
                    : new List<WorkAmendmentsAlternativeTitle>(),

                PerformingArtistNames = workAndAmendment.Original.PerformingArtistNames != null
                    ? workAndAmendment.Original.PerformingArtistNames
                        .Select(x => new WorkAmedmentsPerformingArtistName
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            Firstname = x.Firstname,
                            Lastname = x.Lastname,
                            IPIBaseNumber = x.IPIBaseNumber,
                            IPINameNumber = x.IPINameNumber
                        })
                        .ToList()
                    : new List<WorkAmedmentsPerformingArtistName>(),

                Recordings = workAndAmendment.Original.Recordings != null
                    ? workAndAmendment.Original.Recordings
                        .Select(x => new WorkAmedmentsRecording
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            ISRC = x.ISRC,
                            PerformingArtist = x.PerformingArtist,
                            RecordLabel = x.RecordLabel,
                            ReleaseDate = x.ReleaseDate,
                            Title = x.Title,

                        })
                        .ToList()
                    : new List<WorkAmedmentsRecording>(),

                Splits = workAndAmendment.Original.WorkSplits != null
                    ? workAndAmendment.Original.WorkSplits
                        .Select(x => new WorkAmedmentsSplit
                        {
                            //meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            //data
                            RightToCollect = x.RightToCollect,
                            WriterShare = x.WriterShare,
                            WriterDesignationCode = (ViewEnums.WriterDesignationCodeType)Enum.ToObject(typeof(ViewEnums.WriterDesignationCodeType), x.WriterDesignationCode),
                            Society = x.RelatedWriter.Society != default(DomainModels.Society) ? x.RelatedWriter.Society.Name : string.Empty,

                            // nav
                            SplitWriter = new WorkAmendmentsWriter
                            {
                                ID = x.RelatedWriter.ID,
                                RowVersion = x.RelatedWriter.RowVersion,
                                IsDeleted = x.RelatedWriter.IsDeleted,
                                FirstName = x.RelatedWriter.FirstName,
                                MiddleName = x.RelatedWriter.MiddleName,
                                LastName = x.RelatedWriter.LastName,
                                CaeIpiNumber = x.RelatedWriter.CaeIpiNumber,
                                PublisherCaeIpi = x.RelatedWriter.PublisherCaeIpi,
                                PublisherName = x.RelatedWriter.PublisherName,
                                UniqueCode = x.RelatedWriter.UniqueCode,
                                WriterUnknownIndicator = x.RelatedWriter.WriterUnknownIndicator,
                            }
                        })
                        .ToList()
                    : new List<WorkAmedmentsSplit>(),


            },
            // Updated Track Details
            ActiveAmendmentWork = workAndAmendment.CurrentAmendment == default(WorkGraph.WorkAmendment)
                ? default(ViewModels.WorkAmedmentsComparisonModel)
                : new WorkAmedmentsComparisonModel
                {
                    // meta
                    ID = workAndAmendment.CurrentAmendment.ID,
                    RowVersion = workAndAmendment.CurrentAmendment.RowVersion,
                    IsDeleted = workAndAmendment.CurrentAmendment.IsDeleted,
                    IsNew = workAndAmendment.CurrentAmendment.IsNew,

                    // data
                    Title = workAndAmendment.CurrentAmendment.Title,
                    DurationMins = workAndAmendment.CurrentAmendment.DurationMins,
                    DurationSecs = workAndAmendment.CurrentAmendment.DurationSecs,
                    Iswc = workAndAmendment.CurrentAmendment.Iswc,
                    UniqueCode = workAndAmendment.CurrentAmendment.UniqueCode,
                    Version = workAndAmendment.CurrentAmendment.Version,
                    IsRemix = workAndAmendment.CurrentAmendment.IsRemix,
                    ContainsSample = workAndAmendment.CurrentAmendment.ContainsSamples,

                    // nav items
                    AlternativeTitles = workAndAmendment.CurrentAmendment.AlternativeTitlesAmendments != null
                    ? workAndAmendment.CurrentAmendment.AlternativeTitlesAmendments
                        .Select(x => new WorkAmendmentsAlternativeTitle
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            Title = x.Title
                        })
                        .ToList()
                    : new List<WorkAmendmentsAlternativeTitle>(),
                    PerformingArtistNames = workAndAmendment.CurrentAmendment.PerformingArtistNameAmendments != null
                    ? workAndAmendment.CurrentAmendment.PerformingArtistNameAmendments
                        .Select(x => new WorkAmedmentsPerformingArtistName
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            Firstname = x.Firstname,
                            Lastname = x.Lastname,
                            IPIBaseNumber = x.IPIBaseNumber,
                            IPINameNumber = x.IPINameNumber
                        })
                        .ToList()
                    : new List<WorkAmedmentsPerformingArtistName>(),

                    Recordings = workAndAmendment.CurrentAmendment.RecordingAmendments != null
                    ? workAndAmendment.CurrentAmendment.RecordingAmendments
                        .Select(x => new WorkAmedmentsRecording
                        {
                            // meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            // data
                            ISRC = x.ISRC,
                            PerformingArtist = x.PerformingArtist,
                            RecordLabel = x.RecordLabel,
                            ReleaseDate = x.ReleaseDate,
                            Title = x.Title,
                        })
                        .ToList()
                    : new List<WorkAmedmentsRecording>(),

                    Splits = workAndAmendment.Original.WorkSplits != null
                    ? workAndAmendment.Original.WorkSplits
                        .Select(x => new WorkAmedmentsSplit
                        {
                            //meta
                            ID = x.ID,
                            RowVersion = x.RowVersion,
                            IsDeleted = x.IsDeleted,

                            //data
                            RightToCollect = x.RightToCollect,
                            WriterShare = x.WriterShare,
                            WriterDesignationCode = (ViewEnums.WriterDesignationCodeType)Enum.ToObject(typeof(ViewEnums.WriterDesignationCodeType), x.WriterDesignationCode),
                            Society = x.RelatedWriter.Society != default(DomainModels.Society) ? x.RelatedWriter.Society.Name : string.Empty,

                            // nav
                            SplitWriter = new WorkAmendmentsWriter
                            {
                                ID = x.RelatedWriter.ID,
                                RowVersion = x.RelatedWriter.RowVersion,
                                IsDeleted = x.RelatedWriter.IsDeleted,
                                FirstName = x.RelatedWriter.FirstName,
                                MiddleName = x.RelatedWriter.MiddleName,
                                LastName = x.RelatedWriter.LastName,
                                CaeIpiNumber = x.RelatedWriter.CaeIpiNumber,
                                PublisherCaeIpi = x.RelatedWriter.PublisherCaeIpi,
                                PublisherName = x.RelatedWriter.PublisherName,
                                UniqueCode = x.RelatedWriter.UniqueCode,
                                WriterUnknownIndicator = x.RelatedWriter.WriterUnknownIndicator,
                            }
                        })
                        .ToList()
                    : new List<WorkAmedmentsSplit>(),
                }
        };

        // return
        return viewModel;
    }


};

}

0 个答案:

没有答案