-
-
Save StephenCleary/7deeebf37c91fcf06a30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDependOnProvideContent | |
{ | |
// TODO: consider making this Task<T> instead of setting the view data directly. | |
Task Content { get; set; } | |
} | |
[ProvideContent("Common/Footer")] | |
public class ContactDetailsController : Controller, IDependOnProvideContent | |
{ | |
Task IDependOnProvideContent.Content { get; set; } | |
private Task WaitForContentAsync() | |
{ | |
return ((IDependOnProvideContent)this).Content; | |
} | |
public async Task<ActionResult> Index() | |
{ | |
await WaitForContentAsync(); | |
//omitted for brevity - awaits some other async methods | |
return View(); | |
} | |
} | |
// In ProvideContentAttribute | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
var controller = filterContext.Controller as IDependOnProvideContent; | |
if (controller == null) | |
throw new InvalidOperationException("ProvideContentAttribute controllers must derive from IDependOnProvideContent."); | |
if (filterContext.Result is ViewResult) | |
{ | |
var localPath = filterContext.RouteData.Values["controller"] + "/" + filterContext.RouteData.Values["action"]; | |
if (!_useControllerActionAsPath) | |
localPath = _path; | |
var viewResult = filterContext.Result as ViewResult; | |
controller.Task = GetContentAsync(localPath, viewResult); | |
} | |
} | |
private async Task GetContentAsync(string localPath, ViewResult viewResult) | |
{ | |
var content = await _contentManager.GetContent(localPath); | |
if (viewResult.ViewBag.SystemContent == null) | |
viewResult.ViewBag.SystemContent = new Dictionary<string, MvcHtmlString>(); | |
viewResult.ViewBag.SystemContent[localPath] = new DisplayContentItem(content, localPath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment