The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultController.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.Abstractions;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using Microsoft.AspNetCore.Mvc.ViewEngines;
  8. using Microsoft.AspNetCore.Mvc.ViewFeatures;
  9. using Microsoft.AspNetCore.Routing;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Net.Http.Headers;
  12. using System;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Threading.Tasks;
  16. using System.Web;
  17. using Teknik.Areas.Error.Controllers;
  18. using Teknik.Attributes;
  19. using Teknik.Configuration;
  20. using Teknik.Data;
  21. using Teknik.Filters;
  22. using Teknik.Logging;
  23. using Teknik.Utilities;
  24. namespace Teknik.Controllers
  25. {
  26. [Area("Default")]
  27. [CORSActionFilter]
  28. [ResponseCache(Duration = 3600, Location = ResponseCacheLocation.Any)]
  29. public class DefaultController : Controller
  30. {
  31. protected string Subdomain
  32. {
  33. get { return (string)this.ControllerContext.RouteData.Values["sub"]; }
  34. }
  35. protected readonly ILogger<Logger> _logger;
  36. protected readonly Config _config;
  37. protected readonly TeknikEntities _dbContext;
  38. public DefaultController(ILogger<Logger> logger, Config config, TeknikEntities dbContext)
  39. {
  40. _logger = logger;
  41. _config = config;
  42. _dbContext = dbContext;
  43. ViewBag.Title = string.Empty;
  44. ViewBag.Description = _config.Description;
  45. }
  46. // Get the Favicon
  47. [HttpGet]
  48. [AllowAnonymous]
  49. [ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)]
  50. public IActionResult Favicon([FromServices] IHostingEnvironment env)
  51. {
  52. string imageFile = FileHelper.MapPath(env, Constants.FAVICON_PATH);
  53. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  54. return File(fs, "image/x-icon");
  55. }
  56. // Get the Logo
  57. [HttpGet]
  58. [AllowAnonymous]
  59. [ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)]
  60. public IActionResult Logo([FromServices] IHostingEnvironment env)
  61. {
  62. string imageFile = FileHelper.MapPath(env, Constants.LOGO_PATH);
  63. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  64. return File(fs, "image/svg+xml");
  65. }
  66. // Get the Robots.txt
  67. [HttpGet]
  68. [AllowAnonymous]
  69. public IActionResult Robots([FromServices] IHostingEnvironment env)
  70. {
  71. string dataDir = (string)AppDomain.CurrentDomain.GetData("DataDirectory");
  72. string file = Path.Combine(dataDir, Constants.ROBOTS_PATH);
  73. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  74. return File(fs, "text/plain");
  75. }
  76. protected IActionResult GenerateActionResult(object json)
  77. {
  78. return GenerateActionResult(json, View());
  79. }
  80. protected IActionResult GenerateActionResult(object json, IActionResult result)
  81. {
  82. if (Request.IsAjaxRequest())
  83. {
  84. return Json(json);
  85. }
  86. return result;
  87. }
  88. protected async Task<string> RenderPartialViewToString(ICompositeViewEngine viewEngine, string viewName, object model)
  89. {
  90. if (string.IsNullOrEmpty(viewName))
  91. viewName = ControllerContext.ActionDescriptor.ActionName;
  92. ViewData.Model = model;
  93. using (var writer = new StringWriter())
  94. {
  95. string path = (new Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
  96. ViewEngineResult viewResult =
  97. viewEngine.GetView(path, viewName, false);
  98. ViewContext viewContext = new ViewContext(
  99. ControllerContext,
  100. viewResult.View,
  101. ViewData,
  102. TempData,
  103. writer,
  104. new HtmlHelperOptions()
  105. );
  106. await viewResult.View.RenderAsync(viewContext);
  107. return writer.GetStringBuilder().ToString();
  108. }
  109. }
  110. }
  111. }