Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 319557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:40:20+00:00 2026-05-12T08:40:20+00:00

If you don’t want any context or an example of why I need this,

  • 0

If you don’t want any context or an example of why I need this, then skip to The question(s) at the bottom!

In a bid to keep things tidy I initially built my application without JavaScript. I am now attempting to add a layer of unobtrusive JavaScript on the top of it.

In the spirit of MVC I took advantage of the easy routing and re-routing you can do with things like RedirectToAction().

Suppose I have the following URL to kick off the sign up process:

http://www.mysite.com/signup

And suppose the sign up process is two steps long:

http://www.mysite.com/signup/1
http://www.mysite.com/signup/2

And suppose I want, if JavaScript is enabled, the sign up form to appear in a dialog box like ThickBox.

If the user leaves the sign up process at step 2, but later clicks the “sign up” button, I want this URL:

http://www.mysite.com/signup

To perform some business logic, checking the session. If they left a previous sign up effort half way through then I want to prompt them to resume that or start over.

I might end up with the following methods:

public ActionResult SignUp(int? step)
{
    if(!step.HasValue)
    {
        if((bool)Session["SignUpInProgress"] == true)
        {
            return RedirectToAction("WouldYouLikeToResume");
        }
        else
        {
            step = 1;
        }
    }
    ...
}

public ActionResult WouldYouLikeToResume()
{
    if(Request.IsAjaxRequest())
    {
        return View("WouldYouLikeToResumeControl");
    }
    return View();
}

The logic in WouldYouLikeToResume being:

  • If it’s an AJAX request, only return the user control, or “partial”, so that the modal popup box does not contain the master page.
  • Otherwise return the normal view

This fails, however, because once I redirect out of SignUp, IsAjaxRequest() becomes false.

Obviously there are very easy ways to fix this particular redirect, but I’d like to maintain the knowledge of the Ajax request globally to resolve this issue across my site.

The question(s):

ASP.NET MVC is very, very extensible.

Is it possible to intercept calls to RedirectToAction and inject something like “isAjaxRequest” in the parameters?

OR

Is there some other way I can detect, safely, that the originating call was an AJAX one?

OR

Am I going about this the completely wrong way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:40:20+00:00Added an answer on May 12, 2026 at 8:40 am

    As requested by @joshcomley, an automated answer using the TempData approach:

    This assumes that you have a BaseController and your controllers are inheriting from it.

    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);
        }
    }
    

    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.

    …

    Havn’t tested it though, so be wary of bugs 🙂

    …

    Another generic approach that doesn’t require using state that I can think of may requires you to modify your routes.

    Specifically, you need to be able to add a generic word into your route, i.e.

    {controller}/{action}/{format}.{ajax}.html
    

    And then instead of checking for TempData, you’d check for RouteData[“ajax”] instead.

    And on the extension points, instead of setting the TempData key, you add “ajax” to your RouteData instead.

    See this question on multiple format route for more info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't be afraid to use any technical jargon or low-level explanations for things, please.
I don't know how better to describe this. So take this example. var a
Don't ask me why but i can't use this method because I need to
Don't need to do this right now but thinking about the future... What would
Don't want to sort the entries. using this does not preserve the order as
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
I don't need a whole story to clarify my question, so I'll just show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.