1

If I have a global variable that is false by default but changes later on, is it possible to automatically redirect using this variable? This is basically what I want:

@if (!boolean)
{
  <meta http-equiv='refresh' value='0; redirect_to_this_url'>
}

I've tried this, and it doesn't work (the controller loads forst and throws loads of errors based on this boolean) Please help.

TechnicalTophat
  • 1,655
  • 1
  • 15
  • 37
  • it's not clear. If you want to check a status to stay in current page or redirect it to another page you use lots of way, for instance, you can use cooki and JS to do that. – Mahdi Farhani May 02 '16 at 09:41
  • I don't want to store using cookies, this is just in session cache. But when someone visits my site, I want them to be redirected to a certain page if `boolean` is false. I need this to happen **no matter what page they try to open** – TechnicalTophat May 02 '16 at 09:42
  • Where is the boolean written to? If it is server-side then it would be much better to do this in the Controller rather than the view – Stewart_R May 02 '16 at 09:43
  • @Stewart_R It's server side, and I know it should be in the controller, and that works well. But I want to respect DRY here and only have it in my code once. Is there a sort of 'global controller' that all requests go through? Would it be possible to redirect to a different controller using this? – TechnicalTophat May 02 '16 at 09:44
  • You have a couple of options. Firstly, I should say that Controllers are just classes so you could have a base controller implementing your global redirect functionality then all your other controllers could inherit from it. It would probably be better for you to investigate Filters though: http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs both these options would allow you to stay DRY – Stewart_R May 02 '16 at 09:50
  • you can use custom action filter for your controller , https://msdn.microsoft.com/en-us/library/gg416513(VS.98).aspx Or http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action-filters – Mahdi Farhani May 02 '16 at 09:54
  • you can create Application level variable and store the value. In your view you can this value. So, when this value becomes true, all users who visits the page will be redirected and when it is set to false, no redirection happens. – Karthik M R May 02 '16 at 09:54
  • I think the answers to http://stackoverflow.com/questions/11726848/asp-net-mvc-4-intercept-all-incoming-requests cover your use case – devio May 02 '16 at 09:55
  • Thank you all, but will someone please post this as an answer?? Also, @KarthikMR, that would mean multiple checks, which is a DRY violation – TechnicalTophat May 02 '16 at 09:56
  • you can check only in your layout view – Karthik M R May 02 '16 at 09:57
  • Please reread my question @KarthikMR. I already tried that, but the controller throws an error before the view gets parsed and rendered – TechnicalTophat May 02 '16 at 09:58
  • you can use javascript to redirect. it would work in layout too. eg. if(//check variable) {window.location.href = urlLoginPage;} – Karthik M R May 02 '16 at 10:00
  • I CAN'T USE THE VIEW @KarthikMR. Please listen. This cannot be in the view. It must be in the controller. – TechnicalTophat May 02 '16 at 10:01
  • @Anonymouse - Why can't you use the JS code in your layout view. Any page that user requests will have layout view. So, you are checking the condition. What is the issue with that? – Karthik M R May 02 '16 at 10:05
  • @KarthikMR Because the way ASP.NET MVC sites execute means that the controller gets executed first. If the controller relies on this value (which it does), then the controller will throw an error. – TechnicalTophat May 02 '16 at 10:07
  • @Stewart_R Thank you for your answer. Please could you make an answer for this question? Thanks. – TechnicalTophat May 02 '16 at 10:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110818/discussion-between-karthik-m-r-and-anonymouse). – Karthik M R May 02 '16 at 10:22

1 Answers1

2

This could be tackled using Action Filters

For example, consider what happens when we replace the default FilterConfig.cs file in the App_Start folder of an Asp.Net 4.5.2 MVC project with this:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new RedirectActionFilter());
    }
}

public class RedirectActionFilter : IActionFilter
{
    static Random rng = new Random();
    static bool GetBool() => rng.Next() % 2 == 0;

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {

        if (GetBool())
        {
            filterContext.Result = new RedirectResult("http://www.bbc.co.uk");
        }

    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}

Then the OnActionExecuted() method of our RedirectActionFilter class gets called before the Asp.Net application responds to the browser. As a result, if GetBool() returns true then we will redirect to bbc.co.uk otherwise nothing is changed and we would get the normal action result from whichever controller action is handling our request

Stewart_R
  • 13,764
  • 11
  • 60
  • 106