Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can extend the <code>[Authorize]</code> attribute so that you can override the <code>HandleUnauthorizedRequest</code> function to return a <code>JsonResult</code> to your AJAX call.</p> <pre><code>public class AuthorizeAjaxAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { // It was an AJAX request =&gt; no need to redirect // to the login url, just return a JSON object // pointing to this url so that the redirect is done // on the client var referrer = filterContext.HttpContext.Request.UrlReferrer; filterContext.Result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { redirectTo = FormsAuthentication.LoginUrl + "?ReturnUrl=" + referrer.LocalPath.Replace("/", "%2f") } }; } else base.HandleUnauthorizedRequest(filterContext); } } </code></pre> <p>Create a Javascript function handle the redirect:</p> <pre><code>&lt;script type="text/javascript"&gt; function replaceStatus(result) { // if redirectTo has a value, redirect to the link if (result.redirectTo) { window.location.href = result.redirectTo; } else { // when the AJAX succeeds refresh the mydiv section $('#mydiv').html(result); } }; &lt;/script&gt; </code></pre> <p>And then call that function in the OnSuccess option of your Ajax.ActionLink</p> <pre><code>Ajax.ActionLink("Update Status", "GetStatus", new AjaxOptions { OnSuccess="replaceStatus" }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload