Ayo Adesina
Ayo Adesina

Reputation: 2395

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?

Upvotes: 3

Views: 4761

Answers (1)

Fran
Fran

Reputation: 6520

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.

Upvotes: 2

Related Questions