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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. [CORSActionFilter]
  27. [Area("Default")]
  28. public class DefaultController : Controller
  29. {
  30. protected string Subdomain
  31. {
  32. get { return (string)this.ControllerContext.RouteData.Values["sub"]; }
  33. }
  34. protected readonly ILogger<Logger> _logger;
  35. protected readonly Config _config;
  36. protected readonly TeknikEntities _dbContext;
  37. public DefaultController(ILogger<Logger> logger, Config config, TeknikEntities dbContext)
  38. {
  39. _logger = logger;
  40. _config = config;
  41. _dbContext = dbContext;
  42. ViewBag.Title = _config.Title;
  43. ViewBag.Description = _config.Description;
  44. }
  45. // Get the Favicon
  46. [HttpGet]
  47. [AllowAnonymous]
  48. [ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)]
  49. public IActionResult Favicon([FromServices] IHostingEnvironment env)
  50. {
  51. string imageFile = FileHelper.MapPath(env, Constants.FAVICON_PATH);
  52. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  53. return File(fs, "image/x-icon");
  54. }
  55. // Get the Logo
  56. [HttpGet]
  57. [AllowAnonymous]
  58. [ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)]
  59. public IActionResult Logo([FromServices] IHostingEnvironment env)
  60. {
  61. string imageFile = FileHelper.MapPath(env, Constants.LOGO_PATH);
  62. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  63. return File(fs, "image/svg+xml");
  64. }
  65. // Get the Robots.txt
  66. [HttpGet]
  67. [AllowAnonymous]
  68. public IActionResult Robots([FromServices] IHostingEnvironment env)
  69. {
  70. //string file = FileHelper.MapPath(env, Constants.ROBOTS_PATH);
  71. return File(Constants.ROBOTS_PATH, "text/plain");
  72. }
  73. protected IActionResult GenerateActionResult(object json)
  74. {
  75. return GenerateActionResult(json, View());
  76. }
  77. protected IActionResult GenerateActionResult(object json, IActionResult result)
  78. {
  79. if (Request.IsAjaxRequest())
  80. {
  81. return Json(json);
  82. }
  83. return result;
  84. }
  85. protected async Task<string> RenderPartialViewToString(ICompositeViewEngine viewEngine, string viewName, object model)
  86. {
  87. if (string.IsNullOrEmpty(viewName))
  88. viewName = ControllerContext.ActionDescriptor.ActionName;
  89. ViewData.Model = model;
  90. using (var writer = new StringWriter())
  91. {
  92. string path = (new Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
  93. ViewEngineResult viewResult =
  94. viewEngine.GetView(path, viewName, false);
  95. ViewContext viewContext = new ViewContext(
  96. ControllerContext,
  97. viewResult.View,
  98. ViewData,
  99. TempData,
  100. writer,
  101. new HtmlHelperOptions()
  102. );
  103. await viewResult.View.RenderAsync(viewContext);
  104. return writer.GetStringBuilder().ToString();
  105. }
  106. }
  107. }
  108. }