Data annotation dynamically get parameter from controller

Viewed 150

I would validate if the currenlty active user has permission to acces the company. Below I have two samples of GET and PUT request, to fetch company details and to updated company details. Is it possible to write a Data Annotation so I can point, or send a property to it so I'm able to validate if the user can access it?

Sample:

[ValidateCompanyPermission("companyId")]
[HttpGet]
public async Task<IActionResult> GetCompany(long companyId)
{
    
}

public class CompanyRequest
{
    public long UpdatedById { get; set; }
    public string NewCompanyName
}

[ValidateCompanyPermission("UserRequest.UpdatedById")]
[HttpPut]
public async Task<IActionResult> GetCompanz(CompanyRequest companyRequest)
{
    
}
1 Answers

If sending parameters to the attribute logic is what you want, it's not possible. I quote vcsjones in the same question answered here

Attributes are meta data. Parameter values must be a constant value.

Related