0

I have a little software that gets a parameter by posting it to the controller and return a view according:

 if (TempData[Main.SELF_DEPOSIT_LAYOUT] != null && TempData[Main.SELF_DEPOSIT_LAYOUT] is SelfDepositInit)
        {
            var mainLayout = TempData[Main.SELF_DEPOSIT_LAYOUT] as SelfDepositInit;
            Session["layoutSelfDeposit"] = mainLayout.Layout;
            return View("~/Views/SelfDeposit/" + Session["layoutSelfDeposit"] + "/" + this.ControllerContext.RouteData.Values["action"].ToString() + ".cshtml", model);
        }

My problem is what if the person who post the value is sending an incorrect value and the view/folder doesn't exists? In this case i want to submit a default folder.

How can it be achieved?

Igal C
  • 45
  • 1
  • 9
  • If not found mvc will return 404 not found, so you can look at [this](http://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling). – Berkay Yaylacı Jan 09 '17 at 07:14

1 Answers1

2
 private bool ViewExists(string name)
 {
     ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
     return (result.View != null);
 }

You provide view name in this method to check view existence if it returns null value redirect it to default one

viksnta
  • 21
  • 2