0

我正在开发一个 asp.net MVC3 应用程序。

我有一个LiveFeedControllerwith 方法,它根据用户选择的选项返回项目列表。它根据请求是否为 ajax 请求返回完整视图或部分视图。

例如:

  Public Class LiveFeedController
  Inherits System.Web.Mvc.Controller

        <Authorize(Roles:="CanViewFeed")>
        Function Feed(Optional ByVal feedID As Nullable(Of Guid) = Nothing, Optional ByVal itemID As Nullable(Of Guid) = Nothing, Optional ByVal refreshTimer As Double = 0.0) As ActionResult
            Dim feedVM As New FeedViewModel
            feedVM.SelectedFeedID = feedID 
            feedVM.SelectedItemID = itemID 
            feedVM.RefreshTimer = refreshTimer
            Return View(feedVM)
        End Function

        <Authorize(Roles:="CanViewFeed")>
        <HttpPost()>
        Function Feed(ByVal collection As FormCollection) As ActionResult
            Dim feedVM As New FeedViewModel
            TryUpdateModel(feedVM , collection)
            If Request.IsAjaxRequest Then
                Return PartialView("PartialFeed", feedVM)
            End If
            Return View(feedVM)
        End Function

我的问题是,如果向HttpPost方法提交了ajax请求,并且用户不再登录(所以Authorize失败),返回的内容就是我在web.config文件中指定的登录页面:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

这显示在我的页面/视图的动态部分中,但它使 html 无效,因为返回的内容是登录页面的完整视图。

我也想避免这个问题并正确显示登录页面的内容。我怎样才能做到这一点?

谢谢,

-弗林尼

4

1 回答 1

1

通过阅读此线程,我找到了解决问题的方法:How to manage a redirect request after a jQuery Ajax call。我使用的解决方案未标记为线程的答案。

这就是我所做的。

在我的 Global.asax.vb 文件中,我实现了一个处理PreSendRequestHeaders事件的方法。如果Response.StatusCode指示重定向(302),并且如果请求是 Ajax 请求,我将状态代码更改为“OK”(200)并添加一个标头,指示需要身份验证。我还添加了一个名为 New_Location 的标头,指示浏览器应该重定向到的位置。

Private Sub MvcApplication_PreSendRequestHeaders(sender As Object, e As System.EventArgs) Handles Me.PreSendRequestHeaders
    If Context.Response.StatusCode = 302 And New HttpContextWrapper(Context).Request.IsAjaxRequest Then
        Context.Response.StatusCode = 200
        Context.Response.AddHeader("REQUIRES_AUTH", "1")
        Context.Response.AddHeader("NEW_LOCATION", Context.Response.RedirectLocation)
    End If
End Sub

现在,在处理我的 Ajax 成功事件的函数中,我检查REQUIRES_AUTH标头是否设置为'1',如果是,我将用户重定向到NEW_LOCATION标头中的值。

像这样:

var htmlForm = document.getElementById('feedInfoForm');
var form = $('#feedInfoForm');
$.ajax({
        type: htmlForm.method,
        url: htmlForm.action,
        data: form.serialize(),
        cache: false,
        success: function (result, textStatus, xhr) {

            //Checking if the response should have been a redirect
            if (xhr.getResponseHeader('REQUIRES_AUTH') == '1') {
                window.location = xhr.getResponseHeader('NEW_LOCATION'); //redirecting
                XMLHttpRequest.abort(); // terminating further ajax execution
            }

            $('#dynamicFeedContent').html(result);

        },
        error: function (jqXHR, textStatus, errorThrown) {
           var str = textStatus + " " + errorThrown;
            $('#dynamicFeedContent').html(str);
        }
    });

此解决方案允许我将窗口重定向到完整的登录页面,以避免在我的实时提要内容中间呈现登录页面的问题。

-弗林尼

于 2013-10-28T16:51:53.553 回答