6

I got a problem when debugging my MVC program and I want to acces to my db called "UserActivity". on the browser, it saying that "The localhost page isn’t working

localhost redirected you too many times."

but without showing the specific error location.

here is my UserActivtyController, GET /UserActivity/Index code:

public class UserActivityController : BaseController
{
    //GET /UserActivity/Index
    public ActionResult Index(string returnUrl, int page = 1, string sort = "Id", string sortDir = "ASC", string filter = null)
    {
        String query = @"
            SELECT Id
            ,CreatedBy
            ,CreatedOn
            ,ModifiedBy
            ,ModifiedOn
            ,ContactId
            ,EntityName
            ,EntityId
            ,ActivityType
            ,ActivityStatus
            ,DueDate
            ,ActualEndDate
            ,MasqueradeOn
            ,MasqueradeBy 
        FROM UserActivity 
        -- ORDER BY CreatedOn DESC
        -- OFFSET (@PageNumber -1) * 30 ROWS
        -- FETCH NEXT 30 ROWS ONLY
            ";

        //string countQuery = @""

        List<UserActivityModels> userActivity = null;

        using (IDbConnection db = new MySqlConnection(ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString))
        {
            userActivity = (List<UserActivityModels>)db.Query<UserActivityModels>(query, new
            {
                @PageNumber = page,

            });

            /*ViewData["TotalCount"] = (int)db.ExecuteScalar(countQuery, new
            {
                @PageNumber = page,
                @Id = string.IsNullOrEmpty(filter) ? null : filter
            });
            */

            ViewData["PageSize"] = 30;
            ViewData["Filter"] = filter;
        }

        if (userActivity != null)
        {
            return RedirectToAction(returnUrl);
        }

        return View(userActivity);
    }
}

Really appreciate if there anyone who know something about this problem. Thanks

INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
Botski
  • 195
  • 2
  • 3
  • 13

3 Answers3

5
if (userActivity != null)
{
    return RedirectToAction(returnUrl);
}

If the returnUrl points to the same action ("UserActivity/Index") it will create infinite redirect loop. If you want to redirect request to different action make sure you pass correct name.

Lesmian
  • 3,932
  • 1
  • 17
  • 28
1

You have a loop back situation. This is similar to endless while loop. To fix it change your code redirection implementation to redirect to an action method. Notice how I have changed the implementation below. This will fix the issue "localhost redirected you too many times". You can improve on it to support passing in parameters, etc suitable for your situation. Also take a look at RedirectToAction with support for additional parameters, if you want to pass parameters to the action method, this link will be useful.

    public class UserActivityController : BaseController
    {
        //GET /UserActivity/Index
        public ActionResult Index(int page = 1, string sort = "Id", string sortDir = "ASC", string filter = null)
        {
            // Your other implementation here. I have removed it for brevity.

            if (userActivity != null)
            {
                return RedirectToAction("Index");
            }

            return View(userActivity);
        }

        public ActionResult Index()
        {

          return View();
        }
   }
Community
  • 1
  • 1
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
0

I don't know what is the value of redirectUrl but I suppose it to be null. I also suppose that your userActivity is not null. So return RedirectToAction(returnUrl); gets called.

When you call RedirectToAction(null) you actually redirect to the same action and everything repeats again.

I also am wondering why would you need to return View(userActivity); when your userActivity is null. I suppose you have a logical error.

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34