BuddyJoe
BuddyJoe

Reputation: 71161

ASP.NET MVC - Response.Write code - put it in the Controller, SingleFile or CodeBehind

If you are going to be generating a page as a string and outputting all the content via Response.Write at the last minute where should you put this code?

I need to be able to dynamically write a bunch of JavaScript to the client to use some of the new Google visualization jsapi. I noticed this last release of the MVC framework "discourages" CodeBehinds on the Views. This is really presentation code. Where should it go? Whats the preferred method here... I was never a fan of the "SingleFile" thing.

Upvotes: 3

Views: 5458

Answers (2)

Tomas Aschan
Tomas Aschan

Reputation: 60674

You could also use the ActionResult type Content to return a plain string to the browser.

public ActionResult myAction() {
    Return Content("Hello World!");
}

However, I am not sure whether this is the best approach when returning JavaScript. You might want to consider writing your own JavaScriptResult that inherits ActionResult.

Upvotes: 8

tvanfosson
tvanfosson

Reputation: 532695

Is the code parameterizable? That is can you control the differences between one invocation and another via some small set of parameters? If so, then I'd consider writing an HtmlHelper extension to generate it. Then I would use the extension in my View to generate the code. Pass the parameters to the View via ViewData from the controller if you can't derive it from the Model or RouteValues.

<%= Html.GenerateGoogleVisAPI( ViewData["someThing"],
                               ViewData["otherThing"] ) %>

Another alternative would be to make it into a ViewUserControl -- to which you can pass a Model and ViewData. You'd still include this via the view.

<% Html.RenderPartial( "GoogleVisControl",
                       ViewData["GoogleVisModel"],
                       ViewData ); %>

EDIT:

My HtmlHelperExtensions usually go in a separate class library project. So my hierarchy would look something like the following. Note that I'm omitting the associated Test projects for each level for the sake of brevity, but they would be in there as well. If you have multiple applications using the same data layer, the data layer may exist in a separate solution entirely depending on whether you have one solution per app or multiple apps per solution. I add project references to the web project for the class library projects.

+-Project.Web
 +-Content
 +-Controllers
 +-Models
 +-Views
  +-...
  +-Shared
   +-Error.aspx
   +-GoogleVisControl.ascx
   +-LoginUserControl.ascx
   +-Site.Master
   +-...
  +-...
 +-...
+-Project.Common
+-Project.Common.Web
 +-HtmlHelperExtensions.cs
 +-...
+-Project.Data
 +...

Upvotes: 4

Related Questions