Skip to content

Instantly share code, notes, and snippets.

@StephenCleary
Created April 9, 2015 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenCleary/7deeebf37c91fcf06a30 to your computer and use it in GitHub Desktop.
Save StephenCleary/7deeebf37c91fcf06a30 to your computer and use it in GitHub Desktop.
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