Automatically move the user after a certain period of time in the view

I have a simple MVC 3 application. I want the site to automatically redirect the user to another place after he logs out and is on the exit page for a few seconds. I would like this to be implemented in the view, but I can't figure out how to work with MVC conventions in something to do this. I know I can use this:

<META HTTP-EQUIV="Refresh" CONTENT="5;URL=/Index"> 

But this means that I have to specify the URL, [or if its just / Index adds it to the current URL, that is, it will trigger the controller action (the information in brackets is incorrect)]. The only problem is my account controller, and I do not want to redirect them (users) to one of my actions. I want them to be redirected to an action in my home controller, preferably to an index action. I assume that this can be done using a new action in the controller of my account, I will contact him, and everything that the Action does is redirected to a new view. But this seems like a waste of code. Can I specify the controller directly and the action I want to do this?

EDIT: Decided by myself. What I said about this appeneding / Index for the current URL was wrong, I can specify the Controller action there:

 <META HTTP-EQUIV="Refresh" CONTENT="5;URL=/Home/Index"> 

It worked so far, and I did not need to add information about the local host. This gives me the localhost:xxxxx/Home/Index link localhost:xxxxx/Home/Index What bothered me if you use this:

 <META HTTP-EQUIV="Refresh" CONTENT="5;URL=~/Home/Index"> 

The link becomes localhost:xxxxx/Account/~/Home/Index , which is really odd, as it adds the ~ link to the URL, which usually just means copy the contents in advance and add. However, it looks like the presence of ~ still means copying the contents in advance and adding everything after that, we just add ~ this time too ... There is a part of the link account, since View was called from the account controller and is located in the folder "View the control account."

+7
source share
2 answers

You can use the Url.Action , which will take care of creating the correct URL based on your route settings.

Razor Example:

 <META HTTP-EQUIV="Refresh" CONTENT="5; URL=@ (Url.Action("Index", "Home"))"> 

and using WebForms:

 <META HTTP-EQUIV="Refresh" CONTENT="5;URL=<%= Url.Action("Index", "Home") %>"> 

Alternatively, you can use javascript to do the redirection instead of the meta tag:

 <script type="text/javascript"> window.setTimeout(function() { window.location.href = '@Url.Action("Index", "Home")'; }, 5000); </script> 
+18
source

You can use as below:

 using System.Web.UI; 
public ActionResult Index(){ Redirector("Edit", "Home"); return View(); } private void Redirector(string actionName,string controllerName="") { string js = "window.setTimeout(function() {$('#preloader').show();window.location.href = '@Url.Action("+actionName+", "+controllerName+")';}, 5000);"; Control ctrl = new Control(); ScriptManager.RegisterClientScriptBlock(ctrl, this.GetType(), "redirect", js, true); }
0
source

All Articles