VOOZH about

URL: https://www.nuget.org/packages/Carter.SirenNegotiator/

⇱ NuGet Gallery | Carter.SirenNegotiator 2.1.2




👁 Image
Carter.SirenNegotiator 2.1.2

dotnet add package Carter.SirenNegotiator --version 2.1.2
 
 
NuGet\Install-Package Carter.SirenNegotiator -Version 2.1.2
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Carter.SirenNegotiator" Version="2.1.2" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Carter.SirenNegotiator" Version="2.1.2" />
 
Directory.Packages.props
<PackageReference Include="Carter.SirenNegotiator" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Carter.SirenNegotiator --version 2.1.2
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Carter.SirenNegotiator, 2.1.2"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Carter.SirenNegotiator@2.1.2
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Carter.SirenNegotiator&version=2.1.2
 
Install as a Cake Addin
#tool nuget:?package=Carter.SirenNegotiator&version=2.1.2
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Carter.SirenNegotiator

👁 Build status
👁 NuGet

Carter.SirenNegotiator adds content negotiation for Siren requests to your Carter API.

Installation

Packaage Manager

PM> Install-Package Carter.SirenNegotiator

.Net CLI

$ dotnet add package Carter.SirenNegotiator

Usage

Taken from the sample project.

Create a Response Generator
public class ActorResponseGenerator : ISirenResponseGenerator
{
 public bool CanHandle(Type type)
 {
 var listType = typeof(IEnumerable<Actor>);
 var classType = typeof(Actor);
 return classType.IsAssignableFrom(type) || listType.IsAssignableFrom(type);
 }

 public Siren Generate(object data, Uri uri)
 {
 return data is IEnumerable<Actor> 
 ? Generate((IEnumerable<Actor>) data, uri) 
 : Generate((Actor) data, uri);
 }

 private Siren Generate(IEnumerable<Actor> actors, Uri uri)
 {
 var doc = new Siren
 {
 @class = new [] { "collection" },
 entities = new List<Entity>(),
 properties = new { Count = actors.Count() }
 };

 foreach (var actor in actors)
 {
 var entity = new Entity
 {
 @class = new [] { nameof(Actor) },
 rel = new [] { "item" },
 properties = actor,
 links = new List<Link> { new Link { href = uri + "/" + actor.Id, rel = new [] { "self" } } }
 };

 doc.entities.Add (entity);
 }

 doc.actions = new List<Action> (new []{

 new Action
 {
 name = "create-actor",
 title = "Create Actor",
 method = "POST",
 href = uri.ToString(),
 type = "application/json",
 fields = new List<Field>(new[] {new Field {name = "name", type = "text"}, new Field{name = "age", type = "number"}})
 }
 });

 doc.links = new List<Link> { new Link { href = uri.ToString(), rel = new [] { "self" } } };

 return doc;
 }
 
 private Siren Generate(Actor actor, Uri uri)
 {
 return new Siren
 {
 @class = new [] { nameof(Actor) },
 properties = actor,
 links = new List<Link> { new Link { href = uri.ToString(), rel = new [] { "self" } } },
 actions = new List<Action> (new []{
 new Action
 {
 name = "update-actor",
 title = "Update Actor",
 method = "PUT",
 href = uri.ToString(),
 type = "application/json",
 fields = new List<Field>(new[] {new Field {name = "name", type = "text"}, new Field{name = "age", type = "number"}})
 },
 new Action
 {
 name = "delete-actor",
 title = "Delete Actor",
 method = "DELETE",
 href = uri.ToString()
 }
 })
 };
 }
}

This respose generator can handle a list of actors IList<Actor> or a single Actor.

Register Your Response Generator
public void ConfigureServices(IServiceCollection services)
{
 services.AddSingleton<IActorProvider, ActorProvider>();
 services.AddScoped<ISirenResponseGenerator, ActorResponseGenerator>();
 
 services.AddCarter();
}
Create Your Module
public class ActorsModule : CarterModule
{
 public ActorsModule(IActorProvider actorProvider)
 {
 Get("/actors/{id:int}", async (req, res, routeData) =>
 {
 try
 {
 var person = actorProvider.Get(routeData.As<int>("id"));
 await res.Negotiate(person);
 }
 catch (InvalidOperationException)
 {
 res.StatusCode = 404;
 }
 });
 }
}
Send a Request
$ curl -H "Accept: application/vnd.siren+json" http://localhost:5000/actors/1
{
 "class": [
 "Actor"
 ],
 "properties": {
 "Name": "Brad Pitt",
 "Id": 1,
 "Age": 51
 },
 "actions": [
 {
 "name": "update-actor",
 "title": "Update Actor",
 "method": "PUT",
 "href": "http://localhost:5000/actors/1",
 "type": "application/json",
 "fields": [
 {
 "name": "name",
 "type": "text"
 },
 {
 "name": "age",
 "type": "number"
 }
 ]
 },
 {
 "name": "delete-actor",
 "title": "Delete Actor",
 "method": "DELETE",
 "href": "http://localhost:5000/actors/1"
 }
 ],
 "links": [
 {
 "rel": [
 "self"
 ],
 "href": "http://localhost:5000/actors/1"
 }
 ]
}

Contributing

Carter.SirenNegotiator is a community project. We invite your participation through issues and pull requests!

<a href="https://join.slack.com/t/cartercommunity/shared_invite/enQtMzQwNjIwODcwMTMxLWQwMjk5NDFlYWI3Yzg5Y2M4ODNmOTkwMzA2YjkxNmE0YjI3YWU4MjU2ZjI2NmQwMmE4NjVlODBlM2RlMDI1ZmY"><img src="./slack.svg" width="140px"/></a>

License

Carter.SirenNegotiator is licensed under MIT. Refer to LICENSE for more information.

Product Versions Compatible and additional computed target framework versions.
.NET net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.2 101 6/5/2026
2.1.1 92 6/4/2026
2.0.0 1,351 8/8/2024
1.0.4 1,738 5/2/2018
1.0.3 1,287 5/2/2018
1.0.2 1,530 5/2/2018
1.0.1 1,543 4/26/2018
1.0.0 1,602 4/26/2018