Skip to content

[ControllerAction]

IntelliTect.Coalesce.DataAnnotations.ControllerActionAttribute

Specifies how a custom method is exposed via HTTP. Can be used to customize the HTTP method/verb for the method, as well as caching behavior.

Example Usage

c#
public class Person
{
    public int PersonId { get; set; }
    public string LastName { get; set; }

    public string PictureHash { get; set; }

    [Coalesce]
    [ControllerAction(Method = HttpMethod.Get)]
    public static long PersonCount(AppDbContext db, string lastNameStartsWith = "")
    {
        return db.People.Count(f => f.LastName.StartsWith(lastNameStartsWith));
    }

    [Coalesce]
    [ControllerAction(HttpMethod.Get, VaryByProperty = nameof(PictureHash))]
    public IFile GetPicture(AppDbContext db)
    {
        return new IntelliTect.Coalesce.Models.File(db.PersonPictures
            .Where(x => x.PersonId == this.PersonId)
            .Select(x => x.Content)
        )
        {
            ContentType = "image/jpg",
        };
    }
}

Properties

// Also settable via constructor parameter #1
public HttpMethod Method { get; set; } = HttpMethod.Post;

The HTTP method to use on the generated API Controller.

Enum values are:

  • HttpMethod.Post Use the POST method.
  • HttpMethod.Get Use the GET method.
  • HttpMethod.Put Use the PUT method.
  • HttpMethod.Delete Use the DELETE method.
  • HttpMethod.Patch Use the PATCH method.

public string VaryByProperty { get; set; }

For HTTP GET model instance methods, if VaryByProperty is set to the name of a property on the parent model class, ETag headers based on the value of this property will be used to implement caching. If the client provides a matching If-None-Match Header with the request, the method will not be invoked and HTTP Status `304 Not Modified`` will be returned.

Additionally, if the VaryByProperty is set to a client-exposed property, the value of the property will be included in the query string when performing API calls to invoke the method. If the query string value matches the current value on the model, a long-term Cache-Control header will be set on the response, allowing the client to avoid making future invocations to the same method while the value of the VaryByProperty remains the same.


Coalesce is a free and open-source framework created by IntelliTect to fill our desire to create better apps, faster. IntelliTect is a high-end software architecture and development consulting firm based in Spokane, Washington.

If you're looking for help with your software project, whether it be a Coalesce application, other technologies, or even just an idea, reach out to us at info@intellitect.com — we'd love to start a conversation! Our clients range from Fortune 100 companies to local small businesses and non-profits.