ASP.NET MVC - Use Reflection to find if a Controller Exists

前端 未结 3 800
离开以前
离开以前 2021-02-10 04:23

I\'m having a heck of a time figuring out how to properly implement my 404 redirecting.

If I use the following

 _
Public Class BaseC         


        
3条回答
  •  灰色年华
    2021-02-10 04:48

    That's a very similar problem to mine, but I like your alternate approach.

    I think the reflection as a dynamic filter might be too performance heavy, but I think I have a better way - you can filter allowed actions by a Regex:

    // build up a list of known controllers, so that we don't let users hit ones that don't exist
    var allMvcControllers = 
        from t in typeof(Global).Assembly.GetTypes()
        where t != null &&
            t.IsPublic &&
            !t.IsAbstract &&
            t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
            typeof(IController).IsAssignableFrom(t)
        select t.Name.Substring(0, t.Name.Length - 10);
    
    // create a route constraint that requires the controller to be one of the reflected class names
    var controllerConstraint = new
    {
        controller = "(" + string.Join("|", allMvcControllers.ToArray()) + ")"
    };
    
    // default MVC route
    routes.MapRoute(
        "MVC",
        "{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        controllerConstraint);
    
    // fall back route for unmatched patterns or invalid controller names
    routes.MapRoute(
        "Catch All", 
        "{*url}",
        new { controller = "System", action = "NotFound" });
    

    Then I add to this an additional method on my base Controller:

    protected override void HandleUnknownAction(string actionName)
    {
        this.NotFound(actionName).ExecuteResult(this.ControllerContext);
    }
    

    In this case BaseController.NotFound handles the missing action on a valid controller.

    So finally:

    • {site}/invalid - found by new reflection based filter
    • {site}/valid/notAnAction - found by HandleUnknownAction
    • {site}/valid/action/id - found by checks in code for the id (as before)
    • {site}/valid/action/id/extraPath - found by not matching any route but the catch all

    I think that's all the 404 scenarios covered :-)

提交回复
热议问题