0

I have a filter attribute that checks for a route parameter and if it doesn't exits I apply some logic to check if the current request needs to be redirected to another route.

My code looks like this

Public Overrides Sub OnActionExecuting(filterContext As ActionExecutingContext)
            If Not filterContext.RouteData.Values.ContainsKey("SearchKey") Then
                strSearchKey = strFrom.ToString.Substring(strFrom.ToString.IndexOf("/") + 1)
                strFrom = strFrom.ToString.Substring(0, strFrom.ToString.IndexOf("/") + 1)
                If Not IsNullOrEmpty(strSearchKey) Then
                    Dim values As New Web.Routing.RouteValueDictionary
                    values.Add("SearchKey", strSearchKey)
                    values.Add("From", strFrom)
                    values.Add("controller", "SearchResults")
                    values.Add("action", "Category")
                    filterContext.Result = New RedirectToRouteResult("SearchCategory", values)
                    'filterContext.Result.ExecuteResult(filterContext)
                End If
            End If
End Sub

This is what I'm doing to get it to redirect but the problem is that it causes an infinite loop and I can't find the reason why it just keeps hitting this same route which is the one I'm redirecting from over and over.

mmushtaq
  • 3,430
  • 7
  • 30
  • 47
rojobo
  • 476
  • 4
  • 16
  • 1
    I think you have to check for the action name and controller name before redirecting, otherwise it will cause an infinite loop. Remember taht SearchCategory is an action also. – Majdi Saibi May 19 '17 at 00:12
  • What exactly do you mean?@MajdiSabi – rojobo May 19 '17 at 03:12
  • @MajdiSaibi I'm already checking that... the action with this particular filter is "CategoryMenu" im redirecting it to Search Category but that never happens the infinite loop says the action name still CategoryMenu – rojobo May 19 '17 at 12:14
  • 1
    This happen because of request passes through the same filter, so when you redirect to Category action it again call the OnActionResult method. so somehow you have to avoid that too many redirect. – Mannan Bahelim May 19 '17 at 12:25

0 Answers0