[ASP.NET][ASP.NET Core] How to return controller/action DisplayName attribute with WebAPI in .NET Framework and ASP.NET Core | CodingSchnauzer
 

[ASP.NET][ASP.NET Core] How to return controller/action DisplayName attribute with WebAPI in .NET Framework and ASP.NET Core

| Posted in Uncategorizeda

I noticed this question “Return info of controller and actions with web api in asp coreon Stack Overflow. I am just very curious about what’s the difference between .NET Framework and ASP.NET Core, then I write two examples simply.

Examples are based on .NET Framework / ASP.NET Core WebAPI Project

 

 

Introduction

In .NET Framework:

  1. Get controller DisplayNameattribute from ControllerContext.ControllerDescriptor.GetCustomAttributes() 
  2. Get action DisplayNameattribute from ActionContext.ActionDescriptor.GetCustomAttributes ():
ControllerContext.ControllerDescriptor.GetCustomAttributes<displaynameattribute>()
ActionContext.ActionDescriptor.GetCustomAttributes<displaynameattribute>()

Convert type to DisplayNameAttribute, then we could get DisplayName value directly :

.NET Framework

[DisplayName("Controller Display Name")]
public class ValuesController : ApiController
{
    // GET api/values
    [DisplayName("Action Display Name")]
    public IEnumerable<string> Get()
    {
        var ControllerDisplayName = string.Empty;
        var ActionsDisplayName = string.Empty;

        var ControllerAttributes = ControllerContext.ControllerDescriptor.GetCustomAttributes<DisplayNameAttribute>();
        if (ControllerAttributes.Count > 0)
        {
            ControllerDisplayName = ((DisplayNameAttribute)ControllerAttributes[0]).DisplayName;
        }

        var ActionAttributes = ActionContext.ActionDescriptor.GetCustomAttributes<DisplayNameAttribute>();
        if (ActionAttributes.Count > 0)
        {
            ActionsDisplayName = ((DisplayNameAttribute)ActionAttributes[0]).DisplayName;
        }

        return new string[] { ControllerDisplayName, ActionsDisplayName };
    }
}

 

In ASP.NET Core:

  1. Get Controller DisplayNameAttribute from ControllerContext.ActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true) 
  2. Get Action DisplayNameAttribute from  ControllerContext.ActionDescriptor.MethodInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true) 
ControllerContext.ActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
ControllerContext.ActionDescriptor.MethodInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false);

Convert type to DisplayNameAttribute, then we could get DisplayName value directly :

ASP.NET Core

[DisplayName("Controller Display Name")]
[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    [DisplayName("Action Display Name")]
    public IEnumerable<string> Get()
    {
        var ControllerDisplayName = string.Empty;
        var ActionsDisplayName = string.Empty;

        var ControllerAttributes = ControllerContext.ActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
        if (ControllerAttributes.Length > 0)
        {
            ControllerDisplayName = ((DisplayNameAttribute)ControllerAttributes[0]).DisplayName;
        }

        var ActionAttributes = ControllerContext.ActionDescriptor.MethodInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false);
        if (ActionAttributes.Length > 0)
        {
            ActionsDisplayName = ((DisplayNameAttribute)ActionAttributes[0]).DisplayName;
        }

        return new string[] { ControllerDisplayName, ActionsDisplayName };
    }
}

 

 

Reference:

1. Return info of controller and actions with web api in asp core –  Stack Overflow


1 thought on “[ASP.NET][ASP.NET Core] How to return controller/action DisplayName attribute with WebAPI in .NET Framework and ASP.NET Core”

  1. rabix says:

    (ViewContext.ActionDescriptor as ControllerActionDescriptor).ControllerTypeInfo.GetCustomAttributes();

    (ViewContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes();

    // DONT FORGET TO CAST

Leave a Reply

Your email address will not be published. Required fields are marked *

CodingSchnauzer ©2020. All Rights Reserved.