Getting Conneg to work with both HTML views and JSON in Fubu 1.3

20 views
Skip to first unread message

Kevin Miller

unread,
Jun 13, 2014, 10:59:08 AM6/13/14
to fubumv...@googlegroups.com
Posting this in hopes that it helps another. I have a web service API endpoint which we want to both have a view and support JSON output via Conneg. I first came upon this documentation.


This mostly confused me due to the large surface area I had to play with. It did provide a lot of context about what is going on in Conneg. Unfortunately I could not get the advice in there to work. 

public class AddJsonToViewsPolicy : Policy
{
public AddJsonToViewsPolicy()
{
Where.RespondsToHttpMethod("GET")
.And.ChainMatches(c=>c.InputType().CanBeCastTo<IAlsoOutputJSON>());
ModifyBy(chain =>
{
chain.Input.AddFormatter<JsonFormatter>();
chain.Output.AddFormatter<JsonFormatter>();
});
}
}

Adding this policy actually caused a runtime error in Fubu 1.3 complaining that one of my chains had no input behavior.

Before posting to the list for help I searched and found this thread where we discussed this exact thing. Sadly, I had participated in the thread but forgot about it. RobertTheGrey’s policy lambda does the trick:

//Added to my web FubuRegistry to Ensure that search requests return JSON AND their HTML view 
Policies.Add(x =>
{
x.Conneg.AcceptJson();
x.ModifyBy(chain => chain.OutputJson());
x.Where.ResourceTypeImplements<IAlsoOutputJSON>();
});

Hope this helps a future Fubu spelunker. If I have committed any crimes against Fubu please let me know. The docs say that Conneg will be made easier in Fubu 2.0 and I look forward to that brighter future. 

KevM 

Jeremy D. Miller

unread,
Jun 13, 2014, 11:03:54 AM6/13/14
to fubumv...@googlegroups.com
Just an fyi, this works OOTB in FubuMVC 2.0 without any additional configuration.



-- 
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fubumvc-deve...@googlegroups.com.
To post to this group, send email to fubumv...@googlegroups.com.
Visit this group at http://groups.google.com/group/fubumvc-devel.
For more options, visit https://groups.google.com/d/optout.

Kevin Miller

unread,
Jun 13, 2014, 11:04:49 AM6/13/14
to fubumv...@googlegroups.com
Loving that! 

KevM 
Reply all
Reply to author
Forward
0 new messages