Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As requested by @joshcomley, an automated answer using the TempData approach:</p> <p>This assumes that you have a BaseController and your controllers are inheriting from it.</p> <pre><code>public class AjaxianController : /*Base?*/Controller { private const string AjaxTempKey = "__isAjax"; public bool IsAjax { get { return Request.IsAjaxRequest() || (TempData.ContainsKey(AjaxTempKey)); } } protected override RedirectResult Redirect(string url) { ensureAjaxFlag(); return base.Redirect(url); } protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues) { ensureAjaxFlag(); return base.RedirectToAction(actionName, controllerName, routeValues); } protected override RedirectToRouteResult RedirectToRoute(string routeName, System.Web.Routing.RouteValueDictionary routeValues) { ensureAjaxFlag(); return base.RedirectToRoute(routeName, routeValues); } private void ensureAjaxFlag() { if (IsAjax) TempData[AjaxTempKey] = true; else if (TempData.ContainsKey(AjaxTempKey)) TempData.Remove(AjaxTempKey); } } </code></pre> <p>To use this, make your controller inherit from AjaxianController and use the "IsAjax" property instead of the IsAjaxRequest extension method, then all redirects on the controller will automatically maintain the ajax-or-not flag.</p> <p>...</p> <p>Havn't tested it though, so be wary of bugs :-)</p> <p>...</p> <p>Another generic approach that doesn't require using state that I can think of may requires you to modify your routes.</p> <p>Specifically, you need to be able to add a generic word into your route, i.e.</p> <pre><code>{controller}/{action}/{format}.{ajax}.html </code></pre> <p>And then instead of checking for TempData, you'd check for RouteData["ajax"] instead.</p> <p>And on the extension points, instead of setting the TempData key, you add "ajax" to your RouteData instead.</p> <p>See <a href="https://stackoverflow.com/questions/255876/asp-net-mvc-routes-with-file-extensions">this question on multiple format route</a> for more info.</p>
    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. 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