How do I always return the same View, independent of Controller and Action?

Asked

Viewed 158 times

0

I created a View calling for Manutencao that displays a maintenance page. And whenever the Index is called I display it.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("Manutencao");
    }
}

On ASP.NET MVC 5 as I do forever, independent of any Action or Controller return to View "Maintenance" ? So I avoid line repetition return View("Manutencao") in all Action (has about 80).

I need this because I will leave the page offline during the weekend for a DNS migration I mean, I need you to always return this page until I finish this migration.

  • It depends a lot on what you want to do. If you always want to show this, regardless of anything, you can simply switch routing to always redirect to a action return this view. If you need to do this according to some condition, maybe you should implement a filter, or something like that. No more details your question gets too wide.

  • @LINQ edited explaining, that’s the goal

  • Maniero’s answer then serves.

  • @Leonardobonetti The other option I posted also not be seen? If served I can develop more.

2 answers

2

With additional information it seems that is more or less this:

public class MvcApplication : System.Web.HttpApplication {
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new CheckForDownPage());
    }
    //o resto do global asax
}
public sealed class CheckForDownPage : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Down.htm");
        if (System.IO.File.Exists(path) && IpAddress != "1.2.3.4") {
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.Redirect("~/Down.htm");
            return;
        }
        base.OnActionExecuting(filterContext);
    }
}

Source.

Doesn’t look like that’s what it even that. If you don’t solve I’ll erase here.

I don’t know if it’s the best option but I found this:

routes.MapRoute("Offline", "{controller}/{action}/{id}",
    new {
        action = "Offline",
        controller = "Home",
        id = UrlParameter.Optional
        },
    new { constraint = new OfflineRouteConstraint() });

Source. There are some options there.

I put in the Github for future reference.

Has another proposed solution that can serve for complex comas scenarios. It may be what you want, but think about if you really need all this, the question doesn’t seem to need.

  • when I enter the url "http://localhost:54350" returns me the maintenance action (in the example "Offline"), but when I enter "http://localhost:54350/Home/Index" it returns me the "Index" action normally.

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.