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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Threading.Tasks;
  15. using System.Web;
  16. using Teknik.Areas.Error.Controllers;
  17. using Teknik.Attributes;
  18. using Teknik.Configuration;
  19. using Teknik.Data;
  20. using Teknik.Filters;
  21. using Teknik.Logging;
  22. using Teknik.Security;
  23. using Teknik.Utilities;
  24. namespace Teknik.Controllers
  25. {
  26. [AllowAnonymous]
  27. [CORSActionFilter]
  28. [Area("Default")]
  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. //protected virtual new TeknikPrincipal User
  39. //{
  40. // get { return HttpContext.User as TeknikPrincipal; }
  41. //}
  42. public DefaultController(ILogger<Logger> logger, Config config, TeknikEntities dbContext)
  43. {
  44. _logger = logger;
  45. _config = config;
  46. _dbContext = dbContext;
  47. ViewBag.Title = _config.Title;
  48. ViewBag.Description = _config.Description;
  49. }
  50. // Get the Favicon
  51. [HttpGet]
  52. [AllowAnonymous]
  53. [ResponseCache(Duration = 60 * 60 * 24, Location = ResponseCacheLocation.Any)]
  54. public IActionResult Favicon([FromServices] IHostingEnvironment env)
  55. {
  56. string imageFile = FileHelper.MapPath(env, Constants.FAVICON_PATH);
  57. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  58. return File(fs, "image/x-icon");
  59. }
  60. // Get the Logo
  61. [HttpGet]
  62. [AllowAnonymous]
  63. [ResponseCache(Duration = 60 * 60 * 24, Location = ResponseCacheLocation.Any)]
  64. public IActionResult Logo([FromServices] IHostingEnvironment env)
  65. {
  66. string imageFile = FileHelper.MapPath(env, Constants.LOGO_PATH);
  67. FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
  68. return File(fs, "image/svg+xml");
  69. }
  70. // Get the Robots.txt
  71. [HttpGet]
  72. [AllowAnonymous]
  73. public IActionResult Robots([FromServices] IHostingEnvironment env)
  74. {
  75. //string file = FileHelper.MapPath(env, Constants.ROBOTS_PATH);
  76. return File(Constants.ROBOTS_PATH, "text/plain");
  77. }
  78. protected IActionResult GenerateActionResult(object json)
  79. {
  80. return GenerateActionResult(json, View());
  81. }
  82. protected IActionResult GenerateActionResult(object json, IActionResult result)
  83. {
  84. if (Request.IsAjaxRequest())
  85. {
  86. return Json(json);
  87. }
  88. return result;
  89. }
  90. protected async Task<string> RenderPartialViewToString(ICompositeViewEngine viewEngine, string viewName, object model)
  91. {
  92. if (string.IsNullOrEmpty(viewName))
  93. viewName = ControllerContext.ActionDescriptor.ActionName;
  94. ViewData.Model = model;
  95. using (var writer = new StringWriter())
  96. {
  97. ViewEngineResult viewResult =
  98. viewEngine.FindView(ControllerContext, viewName, false);
  99. ViewContext viewContext = new ViewContext(
  100. ControllerContext,
  101. viewResult.View,
  102. ViewData,
  103. TempData,
  104. writer,
  105. new HtmlHelperOptions()
  106. );
  107. await viewResult.View.RenderAsync(viewContext);
  108. return writer.GetStringBuilder().ToString();
  109. }
  110. }
  111. }
  112. }