Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Response.Redirect or ActionResult from a single MVC method

I have a MVC method that currently returns an ActionResult - I have had to make some changes and based on the business logic I want to do a response.redirect instead.

So I want to do something like this:

public ActionResult Index(CountryHomePageType currentPage)
{
     if (someVar = true)
     {              
         return View();
     }
     else
     {
        Response.redirect("www.website.com")
     }
}

but I can't becuase Resonse.Redirect is not a ActionResult....

How can I get round this?

like image 287
Ayo Adesina Avatar asked Oct 16 '25 15:10

Ayo Adesina


1 Answers

If you are redirecting outside of your current mvc application you can use

return Redirect("<your external url>"); // like "https://www.google.com"

if you want to redirecto back you your homepage you can use

return RedirectToAction("Index", "Home");

assuming you are using the default mvc setup

You might want to also look at ActionFilters if you are making this check in multiple places.

like image 92
Fran Avatar answered Oct 18 '25 07:10

Fran