Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 - Alternative Render based on request
    primarykey
    data
    text
    <p>Trying to implement a system whereby a developer would create a cshtml Razor view as per usual (e.g. a series of @Html.EditorFor(t=>t.MyProperty) calls interspersed with markup etc.) but based on certain headers coming through in the request to differ the response.</p> <p>As an example:</p> <p>Standard user navigates to the action by typing the route into a browser, gets the standard, expected HTML result.</p> <p>Non-human system visits the same URL as the user, but includes an HTTP header to identify itself, gets a custom output. Custom output in this case is JSON representation of the fields defined with the EditorFor calls.</p> <p>I've gotten reasonably far with this by just creating a new helper called CustomEditorFor that dumps out the JSON if hte header's present or defers to HtmlHelper.EditorFor if it's not, but that gives me some limitations - I need each of the fields to be comma separated and wrapped up as a JSON collection but the info available to the helper doesn't appear to give me any context of whether it's first/last etc. in the view.</p> <p>I'm looking into where I need to override to do this relatively cleanly. So far it's looking like I'll need a custom implementation of IView (or more likely subclassing RazorView) and a custom implementation of a ViewEngine (we're already subclassing RazorViewEngine to handle paths etc. so that's not a problem) and it's looking like, to get the absolute ideal result, I may also need to create a subclass of either WebViewPage or WebPageBase to be able to affect what's shown or not shown to the response etc.</p> <p>I do have a couple of limitations here:</p> <ol> <li>I can't use a separate view file for this</li> <li>I can't put the bits for the JSON in the view file because of (1). It needs to render to HTML properly by default and only to JSON where the correct headers are present</li> </ol> <p>Has anyone done anything along these lines? Am I on the right path? Is there a simpler way to do this? Feels incredibly overengineered but I can't see any other way to do this cleanly at the moment.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload