I’m attempting to inject some text (specifically a few lines of javascript code), into the HTML response from Nancy after it’s found the view and processed everything. The After hook seems to be where I want to be doing this. I can access the Response object through the context I’m given, but my attempts to retrieve any data from it fails. It is of type Action<Stream> so I’ve been trying to get it written into memory, then a string as follows:
public HelloModule()
{
After += ctx => {
var stream = new MemoryStream();
ctx.Response.Contents.Invoke(stream);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
}
};
Get["/"] = p => View["Issues/Views/SingleIssue"];
}
But, when this runs I get an exception on the line ctx.Response.Contents(stream);. The exception is an InvalidOperationException which only states that "CSRF is not enabled on this request".
So, my question is: How do I get Nancy to allow me access to the HTML of the current Response its produced? Or am I completely off with how I’m trying to do this? Is there a better way?
Edit 2: This appears to be an issue with the SuperSimpleViewEngine. I just created a new, clean project with a single view. SSVE can reproduce it, but the spark view engine will work just fine.
Edit 1:
As requested some more information. The code above now contains the entire constructor for the module. I have no custom bootstrapper or other logic in the way. The view is a rather simple view using the default SuperSimpleViewEngine, it’s composed exactly as follows:
Master.sshtml
<html>
<head>
<script type="text/javascript" src="/Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-1.8.22.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript" src="/Scripts/dais-exts.js"></script>
<link rel="stylesheet" type="text/css" href="/Content/Style.css" />
<link rel="stylesheet" type="text/css" href="/Content/jquery-ui-1.8.21.custom.css" />
@Section['Head']
</head>
<body>
@Section['Content']
</body>
</html>
SingleIssue.sshtml
@Section['Head']
<title>Hello!</title>
@EndSection
@Section['Content']
<h2>View/Edit Issue</h2>
<form id="issue-form" method="post">
<input type="hidden" name="HttpMethod" value="Put" />
<input type="submit" />
</form>
@EndSection
Answering the question, just so anyone finding this knows what’s going on.
This is actually a bug in Nancy (version 0.11) and only occurs when using the SuperSimpleViewEngine. Unfortunately there’s no easy workarounds or solutions, but a fix has been applied to the source. So, the options to resolve the issue are: