{"id":13429983,"url":"https://github.com/pauldotknopf/JavaScriptViewEngine","last_synced_at":"2025-03-16T04:31:31.778Z","repository":{"id":46334306,"uuid":"52315946","full_name":"pauldotknopf/JavaScriptViewEngine","owner":"pauldotknopf","description":"An ASP.NET MVC ViewEngine for rendering markup in a JavaScript environment. Ideal for React and Angular server-side rendering.","archived":false,"fork":false,"pushed_at":"2017-12-03T18:41:06.000Z","size":7486,"stargazers_count":75,"open_issues_count":1,"forks_count":13,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-03T14:02:44.756Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pauldotknopf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-23T00:09:55.000Z","updated_at":"2024-08-06T14:08:38.000Z","dependencies_parsed_at":"2022-07-22T10:48:33.263Z","dependency_job_id":null,"html_url":"https://github.com/pauldotknopf/JavaScriptViewEngine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldotknopf%2FJavaScriptViewEngine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldotknopf%2FJavaScriptViewEngine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldotknopf%2FJavaScriptViewEngine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldotknopf%2FJavaScriptViewEngine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pauldotknopf","download_url":"https://codeload.github.com/pauldotknopf/JavaScriptViewEngine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:00:48.826Z","updated_at":"2025-03-16T04:31:31.426Z","avatar_url":"https://github.com/pauldotknopf.png","language":"C#","readme":"# JavaScriptViewEngine\n\nAn ASP.NET MVC (MVC Core 1, MVC 6) ViewEngine for rendering markup in a javascript environment. Ideal for React and Angular server-side rendering.\n\n|ASP.NET MVC Core 1 |ASP.NET MVC 5 |\n|:------:|:------:|\n|[![JavaScriptViewEngine.MvcCore1](https://img.shields.io/nuget/v/JavaScriptViewEngine.MvcCore1.svg?style=flat-square\u0026label=JavaScriptViewEngine.MvcCore1)](http://www.nuget.org/packages/JavaScriptViewEngine.MvcCore1/)|[![JavaScriptViewEngine.MvcCore1](https://img.shields.io/nuget/v/JavaScriptViewEngine.Mvc5.svg?style=flat-square\u0026label=JavaScriptViewEngine.Mvc5)](http://www.nuget.org/packages/JavaScriptViewEngine.Mvc5/)|\n\n# Why?\n\nThe main drive behind this is to support isomorphic/universal rendering. The idea is that your ```Model``` will be passed to a javascript method that will render markup in return. Imagine having a react component tree that is hydrated via a single immutable JSON structure, representing the initial state of the service-side rendered page.\n\nThere were existing projects out there that allowed us to render javascript. All of them had their issues.\n\n- NodeServices - https://github.com/aspnet/NodeServices\n  - pros\n    - Supports ASP.NET 5 (ASP.NET Core 1)\n    - .NET Core (Windows/Linux/Mac)\n  - cons\n    - Too much .NET integration than what is needed\n    - Razor is required\n- React.NET - https://github.com/reactjs/React.NET\n  - pros\n    - Embedded javascript engine\n    - Fast\n  - cons\n    - Narrow focus (only React, not Angular)\n    - Limited support for libraries\n    - Opinionated\n    - No .NET Core support.\n\n# Example projects\n\nCheckout the [JavaScriptViewEngine.Samples](https://github.com/pauldotknopf/JavaScriptViewEngine.Samples/tree/master/src) repo!\n\nOr, checkout the [react-aspnet-boilerplate](https://github.com/pauldotknopf/react-aspnet-boilerplate).\n\n# In a nutshell\n\nGetting started is pretty simple.\n\n1. Add a reference to the ```JavaScriptViewEngine``` NuGet package.\n2. Setup things app in your ```Startup.cs```.\n```c#\npublic class Startup\n{\n    private readonly IHostingEnvironment _env;\n\n    public Startup(IHostingEnvironment env)\n    {\n        _env = env;\n    }\n        \n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddJsEngine();\n        services.Configure\u003cRenderPoolOptions\u003e(options =\u003e\n        {\n            options.WatchPath = _env.WebRootPath;\n            options.WatchFiles = new List\u003cstring\u003e\n            {\n                Path.Combine(options.WatchPath, \"default.js\")\n            };\n        });\n        services.AddMvc();\n    }\n\n    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.\n    public void Configure(IApplicationBuilder app)\n    {\n        app.UseJsEngine(); // this needs to be before MVC\n            \n        app.UseMvc(routes =\u003e\n        {\n            routes.MapRoute(\"default\", \"{controller=Home}/{action=Index}/{id?}\");\n        });\n    }\n}\n```\n3. Create ```default.js``` in your ```WebRootPath``` that will be invoked when rendering.\n```javascript\nmodule.exports = {\n    renderView: function (callback, path, model, viewBag, routeValues) {\n        callback(null, {\n            html: \"\u003chtml\u003e\u003chead\u003e\u003c/head\u003e\u003cbody\u003e\u003cp\u003e\u003cstrong\u003eModel:\u003c/strong\u003e \" + JSON.stringify(model) + \"\u003c/p\u003e\u003cp\u003e\u003cstrong\u003eViewBag:\u003c/strong\u003e \" + JSON.stringify(viewBag) + \"\u003c/p\u003e\u003c/body\u003e\",\n            status: 200,\n            redirect: null\n        });\n    },\n    renderPartialView: function (callback, path, model, viewBag, routeValues) {\n        callback(null, {\n            html: \"\u003cp\u003e\u003cstrong\u003eModel:\u003c/strong\u003e \" + JSON.stringify(model) + \"\u003c/p\u003e\u003cp\u003e\u003cstrong\u003eViewBag:\u003c/strong\u003e \" + JSON.stringify(viewBag) + \"\u003c/p\u003e\"\n        });\n    }\n};\n```\n4. Get rolling in MVC.\n```c#\npublic class HomeController : Controller\n{\n    public IActionResult Index(string greeting = \"Hello word!\")\n    {\n        return View(new GreetingViewModel { Greeting = greeting });\n    }\n}\n\npublic class GreetingViewModel\n{\n    public string Greeting { get; set; }\n}\n```\n\n# How to build\nThis project uses Cake for building: [http://cakebuild.net/](http://cakebuild.net/)\n\n## Windows\nFrom a Powershell prompt:\n`PS\u003e ./build.ps1`\n\n\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","Bundling and Minification"],"sub_categories":["Bundling and Minification","css, js帮助工具"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldotknopf%2FJavaScriptViewEngine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpauldotknopf%2FJavaScriptViewEngine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldotknopf%2FJavaScriptViewEngine/lists"}