在MVC3项目中使用自定义表单身份验证的Ajax

时间:2012-11-28 11:41:39

标签: ajax asp.net-mvc-3 form-authentication

我有一个带自定义表单身份验证的MVC3项目。 我认证工作正常(我使用了“HttpContext.Current.User.Identity.IsAuthenticated”属性,以确保它有效)

我在我的表单上使用Ajax:

$(document).ready(function () {
    $.ajax({
        url: '/MyPages/MyControllerFunction',
        type: 'POST',
        success: function (result) { $('#MyJavaTemplate').tmpl(result).appendTo('#MyHtmlTable'); },
        error: function (result) {
            $('#errorDisplay').html(result.responseText);
        }
    })
});

当我到达此页面(并且ajax应该调用此控制器的函数)时,我收到此错误:

HTTP错误404.0 - 未找到 您要查找的资源已被删除,名称已更改或暂时不可用。

我的控制器功能:

[HttpPost]
    public ActionResult MyControllerFunction()
    {
        var MyEntity = MyBusinessLogic.GetByID(1);
        return Json(MyEntity);
    }

(我也尝试添加[授权]属性但没有帮助)

这只发生在我身上,我通过ajax调用控制器的功能。在我将程序更改为使用表单身份验证之前,它一切正常。这就好像用户未经过身份验证(即使它是)

应该解决这个问题?

2 个答案:

答案 0 :(得分:0)

我认为您在该控制器操作上指定了GET,其中应该是POST

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult MyControllerFunction()

答案 1 :(得分:0)

我找到了解决方案!

就在这里: Getting 404s when calling Actions in MVC3 with jQuery

我所要做的就是改变:

url: '/MyPages/MyControllerFunction'

要 url:'@ Url.Action(“MyControllerFunction”,“MyPages”)'