1+ using System . Diagnostics ;
2+ using System . Threading . Tasks ;
3+
4+ using Microsoft . AspNetCore . Hosting ;
5+ using Microsoft . AspNetCore . Html ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . Extensions . Configuration ;
8+
9+ using JavaScriptEngineSwitcher . Sample . AspNetCore31 . Mvc31 . Models ;
10+ using JavaScriptEngineSwitcher . Sample . Logic . Models ;
11+ using JavaScriptEngineSwitcher . Sample . Logic . Services ;
12+
13+ namespace JavaScriptEngineSwitcher . Sample . AspNetCore31 . Mvc31 . Controllers
14+ {
15+ public class HomeController : Controller
16+ {
17+ private readonly FileContentService _fileContentService ;
18+ private readonly JsEvaluationService _jsEvaluationService ;
19+
20+
21+ public HomeController (
22+ IConfigurationRoot configuration ,
23+ IWebHostEnvironment hostingEnvironment ,
24+ JsEvaluationService jsEvaluationService )
25+ {
26+ string textContentDirectoryPath = configuration
27+ . GetSection ( "jsengineswitcher" )
28+ . GetSection ( "Samples" ) [ "TextContentDirectoryPath" ]
29+ ;
30+
31+ _fileContentService = new FileContentService ( textContentDirectoryPath , hostingEnvironment ) ;
32+ _jsEvaluationService = jsEvaluationService ;
33+ }
34+
35+
36+ [ ResponseCache ( CacheProfileName = "CacheCompressedContent5Minutes" ) ]
37+ public IActionResult Index ( )
38+ {
39+ ViewBag . Body = new HtmlString ( _fileContentService . GetFileContent ( "index.html" ) ) ;
40+
41+ return View ( ) ;
42+ }
43+
44+ [ HttpGet ]
45+ [ ResponseCache ( CacheProfileName = "CacheCompressedContent5Minutes" ) ]
46+ public IActionResult Demo ( )
47+ {
48+ var model = _jsEvaluationService . GetInitializationData ( ) ;
49+
50+ return View ( model ) ;
51+ }
52+
53+ [ HttpPost ]
54+ public async Task < IActionResult > Demo ( JsEvaluationViewModel editedModel )
55+ {
56+ var model = _jsEvaluationService . GetInitializationData ( ) ;
57+ await TryUpdateModelAsync ( model , string . Empty , m => m . EngineName , m=> m . Expression ) ;
58+
59+ if ( ModelState . IsValid )
60+ {
61+ model = _jsEvaluationService . Evaluate ( model ) ;
62+
63+ ModelState . Clear ( ) ;
64+ }
65+
66+ return View ( model ) ;
67+ }
68+
69+ [ ResponseCache ( CacheProfileName = "CacheCompressedContent5Minutes" ) ]
70+ public IActionResult Contact ( )
71+ {
72+ ViewBag . Body = new HtmlString ( _fileContentService . GetFileContent ( "contact.html" ) ) ;
73+
74+ return View ( ) ;
75+ }
76+
77+ [ ResponseCache ( Duration = 0 , Location = ResponseCacheLocation . None , NoStore = true ) ]
78+ public IActionResult Error ( )
79+ {
80+ return View ( new ErrorViewModel { RequestId = Activity . Current ? . Id ?? HttpContext . TraceIdentifier } ) ;
81+ }
82+ }
83+ }
0 commit comments