Wednesday, April 15, 2020

Find out what controller is being found with OnActionExecuting in .NET Core 3.1.4.

This follows up on this and explains why I need a Controller for every possible route. Without a Controller to find, this method does not get called.

using System;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Cms.Web.Services.Filters
{
   public class ValidateComponentIdentifier : IActionFilter
   {
      public void OnActionExecuting(ActionExecutingContext context)
      {
         string name = (string)context.RouteData.Values["Controller"];
         if (name == "FalloverFailsafe")
         {
            throw new Exception("There is no endpoint with the name specified.");
         }
      }
      
      public void OnActionExecuted(ActionExecutedContext context)
      {
      }
   }
}

 
 

Loop this in as you might BubbleUpExceptions.

No comments:

Post a Comment