Is there an ASP.Net MVC viewer mechanism that supports JavaScript views?

I would like to generate server-side JavaScript in ASP.Net MVC. Is there a viewing mechanism that supports this? Ideally, I would like to get JavaScript from a URL, for example:

http://myapp/controller/action.js 

I looked at the MonoRail project, and they seem to have this feature, but it lacks documentation very much and I cannot find any ports for ASP.Net MVC.

Edit: The idea is to make the page as standard HTML with a URL, for example:

 http://myapp/controller/action 

and like js (in particular, the ExtJS component) using the first url in the question. There will be only one action in the controller, but two kinds: one for HTML and one for JS.

Edit 2: I basically wanted to achieve the same result as parsing / processing router requests in CakePHP.

+4
source share
6 answers

I wanted to expand this idea to not only view Javascript, but more or less any type of document. To use it, you simply place the views for * .js urls in a subfolder of the view folder of your controller:

 \Views +-\MyController +-\js | +-Index.aspx <- This view will get rendered if you request /MyController/Index.js +-Index.aspx 

The class is a decorator for any type of ViewEngine, so you can use it with NVelocity / WebForms / Whatever:

 public class TypeViewEngine<T> : IViewEngine where T : IViewEngine { private readonly T baseEngine; public T BaseEngine { get { return baseEngine; } } public TypeViewEngine(T baseEngine) { this.baseEngine = baseEngine; } public void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "TypeViewEngine", "{controller}/{action}.{type}", new {controller = "Home", action = "Index", type = "html"} ); } public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName) { var vars = controllerContext.RouteData.Values; if(vars["type"] != null && vars["type"].ToString() != "html") { viewName = string.Format("{0}/{1}", vars["type"], viewName); } return baseEngine.FindView(controllerContext, viewName, masterName); } public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName) { return baseEngine.FindPartialView(controllerContext, partialViewName); } public void ReleaseView(ControllerContext controllerContext, IView view) { baseEngine.ReleaseView(controllerContext, view); } } 

Then in the Global.asax.cs file:

 protected void Application_Start() { var ve = new TypeViewEngine<WebFormViewEngine>(new WebFormViewEngine()); ve.RegisterRoutes(RouteTable.Routes); RegisterRoutes(RouteTable.Routes); ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(ve); } 

Thanks for helping everyone!

+3
source

Based on your edit, I will try with a new answer in which you need json data for ExtJS. I just tested it in the application I'm building and it works great. First you need two routes.

 {controller}/{action}.{format} {controller}/{action} 

Now, the Controller class has a Json method to serialize any object you want, and it is JsonResult, so you can simply use:

 public ActionResult List(string format) { // logic here if (string.IsNullOrEmpty(format)) { return View(); } else if (format == "js") { return Json(object_to_serialize); } } 
+2
source

In the 2007 edition of ASP.NET Futures, a new Managed JScript appeared (I cannot find a newer one). I successfully knocked it over a bit, but be careful, you will probably be forced to do some extra work. .ASPX parsing for JScript is unacceptably buggy.

http://www.microsoft.com/downloads/details.aspx?FamilyId=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en

+2
source

I needed to do something very similar, and I used MVCContrib's nvelocity submission mechanism - technically you could use aspx by default but I found that the nvelocity syntax was much simpler to output the script (don't worry if you haven't used it before - I spent about 10 minutes on it!).

Then you just need to add the route to the route table to handle the direction of your .js url in action!

EDIT

I can’t verify this, since I don’t have a visual studio, but for the route you might have something like this:

 RouteTable.Routes.Add(new Route { Url = "[controller]/[action].js", Defaults = new { controller="home", requestType="javascript" }, // Whatever... RouteHandler = typeof(MvcRouteHandler) }); RouteTable.Routes.Add(new Route { Url = "[controller]/[action]", Defaults = new { controller="home"}, // Whatever... RouteHandler = typeof(MvcRouteHandler) }); 

Requests ending in .js must go through the first route - requests without continuation go to the second.

Then your action can have the requestType parameter:

 public ActionResult MyAction (RequestType requestType) { if(requestType == RequestType.JavaScript) { ... new nvelocity view to render javascript } else { ... } } 

As for the directory structure - you are on your own! Not because I don’t want to be useful, but more than that you have the flexibility to do what you want with it!

+1
source

To do this, you do not necessarily need a viewing mechanism, just return the result of the plain text.

Here is the controller (MonoRail) that I used to display some custom culture settings in a javascript object:

 [ControllerDetails("js")] public class JavascriptController : Controller { private ISessionContext sessionContext; public JavascriptController(ISessionContext sessionContext) { this.sessionContext = sessionContext; } public void CultureInfo() { var scriptformat = @"var cultureInfo = {0};"; var json = Context.Services.JSONSerializer.Serialize(getJSONiZableCultureInfo(sessionContext.CurrentCulture)); RenderText(String.Format(scriptformat, json)); } object getJSONiZableCultureInfo(System.Globalization.CultureInfo culture) { return new { // add more there culture.NumberFormat }; } } 

for more complex things that will look like raw text, any viewing engine will work.

Also, you should not use the .js extension to put the URL in your script tag.

+1
source

If you just want to create javascript based on ViewData, you can create your own result. Here is an example .

0
source

All Articles