ASP.NET MVC - Response.Write code - put it in a controller, SingleFile or CodeBehind

If you are going to generate a page as a string and display all the content through Response.Write at the last minute, where should you put this code?

I need to be able to dynamically write a bunch of JavaScript for the client in order to use some of the new Google jsapi renderings. I noticed that the latest release of the MVC framework is “discouraging” CodeBehinds in views. This is really a presentation code. Where should he go? What is the preferred method here ... I've never been a fan of the "SingleFile" stuff.

+3
source share
2 answers

ActionResult Content, .

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

, , JavaScript. , JavaScriptResult, ActionResult.

+8

? ? , HtmlHelper . , . View ViewData , RouteValues.

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

ViewUserControl - ViewData. .

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

HtmlHelperExtensions . , : , , . , , , . - .

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

Source: https://habr.com/ru/post/1702728/


All Articles