Use FromQuery model binder in output formatter












0















I have a custom OutputFormatter in my .net core project. In there I want to use some info inside the querystring of the initial request.



Inside the controller this is nicely done with the FromQuery modelbinder, giving me an object to work with. I would like to have this object (model) in my output formatter as well.



Can I somehow call FromQuery as an instance or such, so I can pass in the HttpContext or the querystring even, to get the model?



public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// Want a model from my querystring here
}









share|improve this question























  • This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

    – Nkosi
    Nov 19 '18 at 11:27













  • Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

    – Nkosi
    Nov 19 '18 at 11:34











  • Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

    – Bas Slagter
    Nov 19 '18 at 12:06






  • 1





    Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

    – AlesD
    Nov 19 '18 at 20:53






  • 1





    Wonder why my question is downvoted?

    – Bas Slagter
    Nov 23 '18 at 8:49
















0















I have a custom OutputFormatter in my .net core project. In there I want to use some info inside the querystring of the initial request.



Inside the controller this is nicely done with the FromQuery modelbinder, giving me an object to work with. I would like to have this object (model) in my output formatter as well.



Can I somehow call FromQuery as an instance or such, so I can pass in the HttpContext or the querystring even, to get the model?



public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// Want a model from my querystring here
}









share|improve this question























  • This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

    – Nkosi
    Nov 19 '18 at 11:27













  • Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

    – Nkosi
    Nov 19 '18 at 11:34











  • Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

    – Bas Slagter
    Nov 19 '18 at 12:06






  • 1





    Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

    – AlesD
    Nov 19 '18 at 20:53






  • 1





    Wonder why my question is downvoted?

    – Bas Slagter
    Nov 23 '18 at 8:49














0












0








0


1






I have a custom OutputFormatter in my .net core project. In there I want to use some info inside the querystring of the initial request.



Inside the controller this is nicely done with the FromQuery modelbinder, giving me an object to work with. I would like to have this object (model) in my output formatter as well.



Can I somehow call FromQuery as an instance or such, so I can pass in the HttpContext or the querystring even, to get the model?



public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// Want a model from my querystring here
}









share|improve this question














I have a custom OutputFormatter in my .net core project. In there I want to use some info inside the querystring of the initial request.



Inside the controller this is nicely done with the FromQuery modelbinder, giving me an object to work with. I would like to have this object (model) in my output formatter as well.



Can I somehow call FromQuery as an instance or such, so I can pass in the HttpContext or the querystring even, to get the model?



public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// Want a model from my querystring here
}






c# .net-core model-binding outputformatter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 8:51









Bas SlagterBas Slagter

8,58263466




8,58263466













  • This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

    – Nkosi
    Nov 19 '18 at 11:27













  • Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

    – Nkosi
    Nov 19 '18 at 11:34











  • Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

    – Bas Slagter
    Nov 19 '18 at 12:06






  • 1





    Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

    – AlesD
    Nov 19 '18 at 20:53






  • 1





    Wonder why my question is downvoted?

    – Bas Slagter
    Nov 23 '18 at 8:49



















  • This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

    – Nkosi
    Nov 19 '18 at 11:27













  • Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

    – Nkosi
    Nov 19 '18 at 11:34











  • Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

    – Bas Slagter
    Nov 19 '18 at 12:06






  • 1





    Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

    – AlesD
    Nov 19 '18 at 20:53






  • 1





    Wonder why my question is downvoted?

    – Bas Slagter
    Nov 23 '18 at 8:49

















This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

– Nkosi
Nov 19 '18 at 11:27







This looks like an XY problem. FromQuery is a metadata attribute and its instance has no functionality

– Nkosi
Nov 19 '18 at 11:27















Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

– Nkosi
Nov 19 '18 at 11:34





Provide a Minimal, Complete, and Verifiable example of what you have so far to help demonstrate what it is you are trying to achieve.

– Nkosi
Nov 19 '18 at 11:34













Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

– Bas Slagter
Nov 19 '18 at 12:06





Not sure if adding more sample code would clearify my question. Thing is that I now use the querystring from the httprequest to get the data but I would like to have this done automatically like using the FromQuery attribute in a controller.

– Bas Slagter
Nov 19 '18 at 12:06




1




1





Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

– AlesD
Nov 19 '18 at 20:53





Why don't you get the information in the controller and include it in the output object the one that is sent in the context.Object? You so or so do custom formatting so only what you decide will be sent out.

– AlesD
Nov 19 '18 at 20:53




1




1





Wonder why my question is downvoted?

– Bas Slagter
Nov 23 '18 at 8:49





Wonder why my question is downvoted?

– Bas Slagter
Nov 23 '18 at 8:49












1 Answer
1






active

oldest

votes


















1





+50









Use HttpContext.Items. The objects placed in there will be cleaned up at the end of request. And you can pass even more with it, defaulted values or changed values (probably it can be a dangerous point if you are going to change binded object)



// GET api/values
[HttpGet]
public ActionResult Get([FromQuery] QData data)
{
HttpContext.Items["data"] = data;
.......
return Ok(....);
}


Also you can have multiple formatters for different types of requests.



public class Formatter : OutputFormatter
{
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return context.HttpContext.Items["data"] is QData;
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var incoming = context.HttpContext.Items["data"] as QData;
.......
}
}


The same way you can put any other object in Items and work with it in formatter. In case of other object it can be more generic and stable solution as it relies on specific structure.






share|improve this answer
























  • Nice. This seems like an acceptable sollution! Thanks.

    – Bas Slagter
    Nov 23 '18 at 8:48











  • Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

    – ASpirin
    Nov 23 '18 at 10:21











  • Will take it into consideration. Thanks. Points go to you! ;)

    – Bas Slagter
    Nov 26 '18 at 14:08












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334338%2fuse-fromquery-model-binder-in-output-formatter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1





+50









Use HttpContext.Items. The objects placed in there will be cleaned up at the end of request. And you can pass even more with it, defaulted values or changed values (probably it can be a dangerous point if you are going to change binded object)



// GET api/values
[HttpGet]
public ActionResult Get([FromQuery] QData data)
{
HttpContext.Items["data"] = data;
.......
return Ok(....);
}


Also you can have multiple formatters for different types of requests.



public class Formatter : OutputFormatter
{
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return context.HttpContext.Items["data"] is QData;
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var incoming = context.HttpContext.Items["data"] as QData;
.......
}
}


The same way you can put any other object in Items and work with it in formatter. In case of other object it can be more generic and stable solution as it relies on specific structure.






share|improve this answer
























  • Nice. This seems like an acceptable sollution! Thanks.

    – Bas Slagter
    Nov 23 '18 at 8:48











  • Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

    – ASpirin
    Nov 23 '18 at 10:21











  • Will take it into consideration. Thanks. Points go to you! ;)

    – Bas Slagter
    Nov 26 '18 at 14:08
















1





+50









Use HttpContext.Items. The objects placed in there will be cleaned up at the end of request. And you can pass even more with it, defaulted values or changed values (probably it can be a dangerous point if you are going to change binded object)



// GET api/values
[HttpGet]
public ActionResult Get([FromQuery] QData data)
{
HttpContext.Items["data"] = data;
.......
return Ok(....);
}


Also you can have multiple formatters for different types of requests.



public class Formatter : OutputFormatter
{
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return context.HttpContext.Items["data"] is QData;
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var incoming = context.HttpContext.Items["data"] as QData;
.......
}
}


The same way you can put any other object in Items and work with it in formatter. In case of other object it can be more generic and stable solution as it relies on specific structure.






share|improve this answer
























  • Nice. This seems like an acceptable sollution! Thanks.

    – Bas Slagter
    Nov 23 '18 at 8:48











  • Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

    – ASpirin
    Nov 23 '18 at 10:21











  • Will take it into consideration. Thanks. Points go to you! ;)

    – Bas Slagter
    Nov 26 '18 at 14:08














1





+50







1





+50



1




+50





Use HttpContext.Items. The objects placed in there will be cleaned up at the end of request. And you can pass even more with it, defaulted values or changed values (probably it can be a dangerous point if you are going to change binded object)



// GET api/values
[HttpGet]
public ActionResult Get([FromQuery] QData data)
{
HttpContext.Items["data"] = data;
.......
return Ok(....);
}


Also you can have multiple formatters for different types of requests.



public class Formatter : OutputFormatter
{
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return context.HttpContext.Items["data"] is QData;
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var incoming = context.HttpContext.Items["data"] as QData;
.......
}
}


The same way you can put any other object in Items and work with it in formatter. In case of other object it can be more generic and stable solution as it relies on specific structure.






share|improve this answer













Use HttpContext.Items. The objects placed in there will be cleaned up at the end of request. And you can pass even more with it, defaulted values or changed values (probably it can be a dangerous point if you are going to change binded object)



// GET api/values
[HttpGet]
public ActionResult Get([FromQuery] QData data)
{
HttpContext.Items["data"] = data;
.......
return Ok(....);
}


Also you can have multiple formatters for different types of requests.



public class Formatter : OutputFormatter
{
public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return context.HttpContext.Items["data"] is QData;
}
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var incoming = context.HttpContext.Items["data"] as QData;
.......
}
}


The same way you can put any other object in Items and work with it in formatter. In case of other object it can be more generic and stable solution as it relies on specific structure.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 15:28









ASpirinASpirin

2,24811224




2,24811224













  • Nice. This seems like an acceptable sollution! Thanks.

    – Bas Slagter
    Nov 23 '18 at 8:48











  • Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

    – ASpirin
    Nov 23 '18 at 10:21











  • Will take it into consideration. Thanks. Points go to you! ;)

    – Bas Slagter
    Nov 26 '18 at 14:08



















  • Nice. This seems like an acceptable sollution! Thanks.

    – Bas Slagter
    Nov 23 '18 at 8:48











  • Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

    – ASpirin
    Nov 23 '18 at 10:21











  • Will take it into consideration. Thanks. Points go to you! ;)

    – Bas Slagter
    Nov 26 '18 at 14:08

















Nice. This seems like an acceptable sollution! Thanks.

– Bas Slagter
Nov 23 '18 at 8:48





Nice. This seems like an acceptable sollution! Thanks.

– Bas Slagter
Nov 23 '18 at 8:48













Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

– ASpirin
Nov 23 '18 at 10:21





Good. As a sugestion: maybe, it will be better, if you'll not pass the query object as it is, but form and pass some kind of FormatterHint object, that will be more general approach. Anyway it is you to decide

– ASpirin
Nov 23 '18 at 10:21













Will take it into consideration. Thanks. Points go to you! ;)

– Bas Slagter
Nov 26 '18 at 14:08





Will take it into consideration. Thanks. Points go to you! ;)

– Bas Slagter
Nov 26 '18 at 14:08




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334338%2fuse-fromquery-model-binder-in-output-formatter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine