我看到MVC 3方法返回一個JsonResult與Authorize屬性一起使用時的奇怪行爲。看起來會發生的情況是,當我沒有登錄但授權是正確評估的,而不是重定向到登錄表單時,Json響應是登錄表單。是否有一個附加屬性指示響應不返回值,而是將用戶重定向到登錄表單,優先使用正確的returnUrl值?我做的一個演示是建立一個新的MVC3站點,並使用aspnet_regsql.exe命令將AspNetMembership添加到我的數據庫。所有設置和正確登錄我。 JsonResult的行爲看起來不正確,我希望我錯過了一個屬性以使其正常工作。任何幫助非常感謝,事先感謝。MVC JsonResult與[Authorize]屬性去登錄但不顯示視圖
這裏是帳戶控制器(不包括Post問題)。
public class AccountController : Controller
{
public ActionResult LogOn()
{
return View();
}
[Authorize]
public JsonResult AuthorizedAction()
{
return Json("Only returns if I am authorized");
}
}
下面是HTML標記:
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btnTest").click(function() {
$.ajax({
type: "POST",
url: "Account/AuthorizedAction",
data: {},
success: function (result) {
$("#testMe").html(result);
},
error: function (result) {
$("#testMe").html('Something broke in the ajax request');
}
});
});
});
</script>
<input type="button" id="btnTest" value="Test me" />
<div id="testMe">I have initial text</div>
結果:
1)登錄後我得到了我的測試DIV 2 '只有當我被授權返回')當沒有登錄,我在我的Logon()方法中有一個斷點我看到這個值 請求[「returnUrl」] 「/ Account/AuthorizedAction」
我有測試div顯示登錄窗體:)這看起來像我只是沒有正確處理。
感謝Darin,非常有幫助。 – likestoski 2012-03-22 18:08:58