frc
frc

Reputation: 588

Method returning redirect to itself - localhost redirected too many times

I have a method like so:

// GET: Pages Index
        public ActionResult Index(string page = "")
        {
            if (page == "")
                page = "home";

            PagesVM pageVM;
            PagesDTO pageDTO;

            Db db = new Db();

            if (!db.Pages.Any(row => row.Slug.Equals(page)))
                return RedirectToAction("Index");

            ...
        }

If I go to mydomain/somepage and that page exists, it works fine, but if the page does not exist it redirect to itself but too many times, I don't get why because when it redirects to itself page should become home and it should work but it keeps redirecting too many times.

Any way to avoid this and basically keep it all in one method?

Upvotes: 2

Views: 1143

Answers (1)

Daniel Mackenzie
Daniel Mackenzie

Reputation: 214

Try putting a break point in at your return statement and running in debug mode, hover over the "page" variable, when the debugger stops the code execution and confirm its value is "home" at the moment the page redirects.

If this is the case, then the issue lies in either your LINQ statement or your data set. Make sure Pages table contains a slug value of home.

Edit:

Replacing

return RedirectToAction("Index"); 

With either

return RedirectToAction("Index",new { page = "" });

Or

return RedirectToAction("Index",new { page = "home" });

Solves this issue.

Upvotes: 1

Related Questions