Attribute-based routing for handling incoming server messages (similar to AspnetCore)
See original GitHub issueDescribe the feature request
Since the server has the capability to listen to messages directly as they come in, I think it would be a good idea to borrow the concept of attribute-based routing from the AspnetCore framework. The idea would be that we create a MqttController and MqttRoute attribute which works in a way that is similar to the AspNet framework. The matching would be done against the path of the Mqtt messages and the appropriate action would be invoked by the MqttServer.
Which project is your feature request related to?
- MQTTnet.AspnetCore
Describe the solution you’d like
- Create a base MqttController
- Create MqttController attribute
- Create MqttRoute attribute
- Add
MapMqttControllersconfiguration as a chainable config toUseMqttEndpoint config in MQTTnet.AspnetCore. - If there are any configured controllers, match them against a routing table and invoke the appropriate Action
Describe alternatives you’ve considered
Alternatives are:
- Do nothing (everyone implements whatever they want in user space)
- Issue guidance in the form of a separate GitHub project and have everyone re-implement as needed on their own project
- Plug into the AspNet endpoint routing directly (not a 100% fit of the paradigm)
Additional context
If this feature is part of the AspNet integration package and optional (needs to use .MapMqttControllers to opt into this behavior, I believe it will be fully backwards compatible and offer a nicer onboarding for new users who are used to the AspNet routing paradigm.
I would be happy to do the implementation if this is something the project would like to add.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
@avishnyak Sure we already mentioned other libs in the readme etc.
I have incorporated all of this feedback in the latest commits and published the beta NuGet package at https://www.nuget.org/packages/MQTTnet.AspNetCore.AttributeRouting.
I have moved the MqttServerApplicationMessageInterceptorDelegate to it’s own singleton called MqttRouter.cs.
Regarding the base controller, I have taken the same approach as AspNetCore. You can either inherit from the MqttBaseController or create your own controller with a MqttController attribute and a property marked with an MqttControllerContext attribute for injection of the incoming message context.
Would you both be ok with me submitting a PR here to add some documentation directing folks who may be interested in this style of programming to the examples and the other repo? I’m thinking some wiki entries and maybe the readme.