commodore73

Generic Entry Controller Action

Jul 24th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. namespace Root.Apps.Web.Csweb.Controllers
  2. {
  3.     using System;
  4.     using System.Diagnostics;
  5.  
  6.     using Microsoft.AspNetCore.Mvc;
  7.     using Microsoft.Extensions.Logging;
  8.  
  9.     using Contentstack.Core;
  10.  
  11.     using Deliverystack.Core;
  12.     using Deliverystack.Core.Models.Repositories;
  13.  
  14.     using Root.Apps.Web.Csweb.Models;
  15.     using Root.Core.Models.Entries;
  16.  
  17.     public class EntryController : Controller
  18.     {
  19.         private readonly ILogger<EntryController> _logger;
  20.         private IRepository _repository;
  21.         private ContentstackClient _stack;
  22.  
  23.         public EntryController(
  24.             IRepository repository,
  25.             ILogger<EntryController> logger,
  26.             ContentstackClient stack)
  27.         {
  28.             _repository = repository;
  29.             _logger = logger;
  30.             _stack = stack;
  31.         }
  32.  
  33.         public IActionResult Privacy()
  34.         {
  35.             return View();
  36.         }
  37.  
  38.         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  39.         public IActionResult Error()
  40.         {
  41.             return View("Error", new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
  42.         }
  43.  
  44.         public IActionResult Index(string url)
  45.         {
  46.             if (string.IsNullOrEmpty(url) || !url.StartsWith("/"))
  47.             {
  48.                 url = "/" + url;
  49.             }
  50.  
  51.             new Instrument(_logger).Track(this, "INSIDE CONTROLLER");
  52.  
  53. //            new Instrument().Track(this, "Index({0})", url);
  54.             WebpageEntry? entryModel = null;
  55.  
  56.             // url == HttpContext.Request.Path
  57.             try
  58.             {
  59.                 entryModel = _repository.GetEntryModel(url) as WebpageEntry;
  60.  
  61.                 if (entryModel == null)
  62.                 {
  63.                     new Instrument().Warn(this, "Unable to locate entry by URL: {0}", url);
  64.                 }
  65.             }
  66.             catch (Exception ex)
  67.             {
  68.                 throw ex;
  69.                 //                new Instrument().Exception(this, ex, "exception retrieving entry model for {0} : ", url); //TODO: ilogger
  70. //                Console.WriteLine(ex + " : " + ex.Message);
  71.                 //To see the exception in the browser in development throw ex;
  72. //                return Error();
  73.             }
  74.  
  75.             if (entryModel != null)
  76.             {
  77.                 //TODO: why is .ContentTypeUid empty/null here?
  78. //                new Instrument().Debug(this, "model for {0} [{1}] : {2}", url, entryModel.ContentTypeUid, entryModel);
  79.  
  80.                 if (!String.IsNullOrEmpty(entryModel.PageData.View))
  81.                 {
  82.                     return View(entryModel.PageData.View, entryModel);
  83.                 }
  84.  
  85.                 return View(entryModel);
  86.             }
  87.             else
  88.             {
  89. //                throw new HttpException(404, "Not Found");
  90.  //               Response.Clear();
  91.   //              Response.StatusCode = 404;
  92.     //           Response.End();
  93.             }
  94.  
  95.             return View("index");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment