Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 792317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:58:22+00:00 2026-05-14T21:58:22+00:00

I have an ASP.NET MVC 2 application in which I am creating a custom

  • 0

I have an ASP.NET MVC 2 application in which I am creating a custom action filter. This filter sits on the controllers in the application and verifies from the database whether that function is currently available.

Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
  Try
    ' Check controller name against database.
    Dim controllerName = filterContext.Controller.GetType().Name
    controllerName = controllerName.Remove(controllerName.Length - 10)
    ' Look up availability.
    Dim available As Boolean = _coreService.GetControllerAvailability(controllerName)
    If Not available Then
      ' Redirect to unavailable notice.
      filterContext.Result = New RedirectResult("/Home/Unavailable/")
    End If
  Catch ex As Exception
    _eventLogger.LogWarning(ex, EventLogEntryType.Error)
    Throw
  End Try
End Sub

My problem is that depending on the action that has been requested I need to redirect the user to an action that returns either a view, partial views or JSON.

Given the ActionExecutingContext can I find out what the return type of the originally requested action is?

EDIT:

Ok, I’m getting closer but have another problem.

Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
  Try
    ' Check controller name against database.
    Dim controllerName = filterContext.Controller.GetType().Name
    Dim shortName = controllerName.Remove(controllerName.Length - 10)
    ' Look up availability.
    Dim available As Boolean = _coreService.GetControllerAvailability(shortName)
    If Not available Then
      ' find out what type is expected to be returned
      Dim actionName As String = filterContext.ActionDescriptor.ActionName
      Dim controllerType = Type.GetType("Attenda.Stargate.Web." & controllerName)
      Dim actionMethodInfo = controllerType.GetMethod(actionName)
      Dim actionReturnType = actionMethodInfo.ReturnType.Name

      Select Case actionReturnType
        Case "PartialViewResult"
          filterContext.Result = New RedirectResult("/Home/UnavailablePartial/")
        Case "JsonResult"
          filterContext.Result = New RedirectResult("/Home/UnavailableJson/")
        Case Else
          filterContext.Result = New RedirectResult("/Home/Unavailable/")
      End Select

    End If
  Catch ex As Exception
    _eventLogger.LogWarning(ex, EventLogEntryType.Error)
    Throw
  End Try
End Sub

I can use reflection to find the return type of the action method. My problem is if I have the following methods on a controller:

Public Function Create() As ViewResult
  Return View()
End Function

<AcceptVerbs(HttpVerbs.Post)>
Public Function Create(values as FormCollection) As ViewResult
  ' Do stuff here
End Function

I get an AmbiguousMatchException thrown.

With the information I have in the OnActionExecuting method, is there anyway of being more precise with determining the overload that is being called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:58:22+00:00Added an answer on May 14, 2026 at 9:58 pm

    Ok, this is the solution I have come up with.

    Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
      Try
        ' Check controller name against database.
        Dim controllerName = filterContext.Controller.GetType().Name
        Dim shortName = controllerName.Remove(controllerName.Length - 10)
        ' Look up availability.
        Dim available As Boolean = _coreService.GetControllerAvailability(shortName)
        If Not available Then
          ' find out what type is expected to be returned
          Dim actionName As String = filterContext.ActionDescriptor.ActionName
          Dim controllerType = Type.GetType("Attenda.Stargate.Web." & controllerName)
          Dim actionMethodInfo As MethodInfo
          Try
            actionMethodInfo = controllerType.GetMethod(actionName)
          Catch ex As AmbiguousMatchException
            ' Try to find a match using the parameters passed through
            Dim actionParams = filterContext.ActionParameters
            Dim paramTypes As New List(Of Type)
            For Each p In actionParams
              paramTypes.Add(p.Value.GetType())
            Next
            actionMethodInfo = controllerType.GetMethod(actionName, paramTypes.ToArray)
          End Try
          Dim actionReturnType = actionMethodInfo.ReturnType.Name
    
          Select Case actionReturnType
            Case "PartialViewResult"
              filterContext.Result = New RedirectResult("/Home/UnavailablePartial/")
            Case "JsonResult"
              filterContext.Result = New RedirectResult("/Home/UnavailableJson/")
            Case Else
              filterContext.Result = New RedirectResult("/Home/Unavailable/")
          End Select
    
        End If
      Catch ex As Exception
        _eventLogger.LogWarning(ex, EventLogEntryType.Error)
        Throw
      End Try
    End Sub
    

    If the Type.GetMethod(string) call fails to identify the method requested, I fetch the parameters collection from the ActionExecutingContext.ActionParameters collection and build an array of the types of the parameters passed in the request. I can then use the Type.GetMethod(string,type()) overload to be more specific about my request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.