Skip to content

Commit bc4cffd

Browse files
committed
2 parents bdcca34 + 39b244f commit bc4cffd

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

README.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[![NuGet Badge](https://buildstats.info/nuget/MQTTnet.AspNetCore.Routing)](https://www.nuget.org/packages/MQTTnet.AspNetCore.Routing)
2-
[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.Routing/LICENSE)
2+
[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://github.com/IoTSharp/MQTTnet.AspNetCore.Routing/LICENSE)
33
[![Build status](https://ci.appveyor.com/api/projects/status/7m482221qoqvxq0j/branch/master?svg=true)](https://ci.appveyor.com/project/MaiKeBing/mqttnet-aspnetcore-routing/branch/master)
44

55
# MQTTnet AspNetCore Routing
66

7-
MQTTnet AspNetCore Routing is a fork of https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.Routing
7+
MQTTnet AspNetCore Routing is a fork of https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting
88

99
This addon to MQTTnet provides the ability to define controllers and use attribute-based routing against message topics in a manner that is very similar to AspNet Core.
1010

@@ -81,7 +81,13 @@ builder.Services
8181
By default, all controllers within the executing assembly are
8282
discovered (just pass nothing here). To provide a list of assemblies
8383
explicitly, pass an array of Assembly[] here.
84-
*/);
84+
*/)
85+
/*
86+
optionally, set System.Text.Json serialization default for use with
87+
[FromPayload] in the controllers. We can specify a JsonSerializerOptions
88+
or use JsonSerializerDefaults, useful for case-sensitivity or comment-handling
89+
*/
90+
.AddMqttDefaultJsonOptions(new JsonSerializerOptions(JsonSerializerDefaults.Web));
8591

8692
var app = builder.Build();
8793

@@ -129,6 +135,23 @@ public class MqttWeatherForecastController : MqttBaseController // Inherit from
129135

130136
return Ok();
131137
}
138+
139+
// Supports binding JSON message payload to parameters with [FromPayload] attribute,
140+
// Similar to ASP.NET Core [FromBody]
141+
[MqttRoute("{deviceName}/telemetry")]
142+
public async Task NewTelemetry(string deviceName, [FromPayload] Telemetry telemetry)
143+
{
144+
// here telemetry is JSON-deserialized from message payload to type Telemetry
145+
bool success = await DoSomething(telemetry);
146+
if (success) {
147+
await Ok();
148+
return;
149+
}
150+
else {
151+
await BadMessage();
152+
return;
153+
}
154+
}
132155
}
133156
```
134157

@@ -142,7 +165,7 @@ Contributions are welcome. Please open an issue to discuss your idea prior to se
142165

143166
## MIT License
144167

145-
See https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.Routing/LICENSE.
168+
See https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting/LICENSE.
146169

147170
## About Atlas LiftTech
148171

0 commit comments

Comments
 (0)