Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Response.Redirect work in a private void MVC 2 Function?

I have a private void function set for some validation. Should my validation fail, I would like to redirect to another ActionResult and kill the process for the ActionResult that was being used. Response.Redirect("controllerName") does not help. Any ideas?

[Accept(HttpVerbs.Post)]
public ActionResult NerdDinner(string Name)
{
   testName(Name);
   ...
   Return RedirectToAction("ActionResultAAA"); 
}

private void testName(string name)  
{
    if(name == null)
    {
        //Response.Redirect("ActionResultBBB");
    }
}
like image 295
MrM Avatar asked May 13 '10 12:05

MrM


1 Answers

You can use Response.Redirect wherever you like but you need to provide a proper (relative or abolute) URL, not just an action name. However, it would be preferable to stick to the MVC pattern and do something like this:

[Accept(HttpVerbs.Post)] 
public ActionResult NerdDinner(string Name) 
{ 
   ActionResult testResult = testName(Name)
   if (testResult != null) return testResult;
   ... 
   return RedirectToAction("ActionResultAAA"); 
} 

private ActionResult testName(string name) 
{ 
    if(name == null) 
    { 
        return RedirectToAction("ActionResultBBB"); 
    } 

    return null;
}
like image 168
Daniel Renshaw Avatar answered Sep 30 '22 04:09

Daniel Renshaw