Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Last active October 11, 2021 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/3bb9daf04f66c9547fc6f11744e7438e to your computer and use it in GitHub Desktop.
Save RickStrahl/3bb9daf04f66c9547fc6f11744e7438e to your computer and use it in GitHub Desktop.

This doesn't work.

Base class:

/// <summary>
/// Handles the Administration Form API backend tasks
/// </summary>
[Route("api/LocalizationAdministration")]
[UnhandledApiExceptionFilter]    
[NonController]
public class LocalizationAdministrationController : Controller
{
    public LocalizationAdministrationController(IHostingEnvironment host, DbResourceConfiguration config)
    {
        Host = host;
        DbIRes = new DbResInstance(config);
    }

    [HttpGet]
    [Route("GetResourceList")]
    //public IEnumerable<ResourceIdItem> GetResourceList(string resourceSet)
    public ActionResult GetResourceList(string resourceSet)
    {
        var ids = Manager.GetAllResourceIds(resourceSet);
        if (ids == null)
            throw new ApplicationException(DbIRes.T("ResourceSetLoadingFailed", STR_RESOURCESET) + ":" +
                                           Manager.ErrorMessage);
        return Json(ids, jsonSettings);
    }

    ...
}

Subclassed:

[Controller]
public class LocalizationAdminController : LocalizationAdministrationController
{
    public LocalizationAdminController(IHostingEnvironment host, DbResourceConfiguration config) : base(host, config)
    {

    }
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);
        Debug.WriteLine("executing subclassed controller");
    }
}

the subclassed controller never fires and client gets a 404.

This works with an abstract class:

Base class:

/// <summary>
/// Handles the Administration Form API backend tasks
/// </summary>
[Route("api/LocalizationAdministration")]
[UnhandledApiExceptionFilter]    
public abstract class LocalizationAdministrationController : Controller
{
    public LocalizationAdministrationController(IHostingEnvironment host, DbResourceConfiguration config)
    {
        Host = host;
        DbIRes = new DbResInstance(config);
    }

    [HttpGet]
    [Route("GetResourceList")]
    //public IEnumerable<ResourceIdItem> GetResourceList(string resourceSet)
    public ActionResult GetResourceList(string resourceSet)
    {
        var ids = Manager.GetAllResourceIds(resourceSet);
        if (ids == null)
            throw new ApplicationException(DbIRes.T("ResourceSetLoadingFailed", STR_RESOURCESET) + ":" +
                                           Manager.ErrorMessage);
        return Json(ids, jsonSettings);
    }

    ...
}

Subclassed:

// [Authorize]
[Controller]
public class LocalizationAdminController : LocalizationAdministrationController
{
    public LocalizationAdminController(IHostingEnvironment host, DbResourceConfiguration config) : base(host, config)
    {
    }
    
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);
        Debug.WriteLine("executing subclassed controller");
    }
}

This allows the user to manage the controller themselves with an [Authorized] attribute or whatever logic they want to add. It also allows overriding of behavior which is nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment