How to get the current ASP.NET core controller method name inside the controller using Reflection or another accurate method

后端 未结 5 1573
深忆病人
深忆病人 2021-02-12 17:40

I want to get the current method name of my ASP.NET Core controller

I have tried getting the method name through reflection:

    [HttpGet]
         


        
5条回答
  •  猫巷女王i
    2021-02-12 17:59

    The C# 5.0 CallerMemberName attribute may do the trick. (I haven't tested this from an async method; it works from a regular call)

    private static string GetCallerMemberName([CallerMemberName]string name = "")
    {
        return name;
    }
    

    Then call it from your code:

    [HttpGet]
    public async Task CreateProcess(int catId)
    {
        string methodName = GetCallerMemberName();
    

    Note that you don't need to pass anything to the method.

提交回复
热议问题