Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Josh Evenson 10 posts 113 karma points
    Apr 06, 2017 @ 20:14
    Josh Evenson
    0

    Load View Configuration Asynchronously

    I have a configuration that I need to pass to the master layout file. This configuration has data that gets displayed in the header, footer, and other various places in the application. I need this configuration to get injected into the master layout file for every request to the application.

    However, I need to load this configuration from a Web API, and I'd like the code to be asynchronous. I haven't had much luck finding a solution.

    So far I've tried:

    • Implementing an Async Action Filter, this isn't working.
    • Rendering a partial view, this does not work because in MVC 5 you cannot render partial views asynchronously
    • @Html.RenderAction, this will also not work for the same reason as above.
    • Overriding the default base controller with DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(MyCustomBaseController)); but this does not have an asynchronous OnActionExecuting method

    Any help is much appreciated, I'm pretty stuck here.

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Apr 07, 2017 @ 08:29
    Damiaan
    1

    Not really answering the questions. But I am wondering why you are stuck on "async". Async is good for going to slow devices like hdd/network. So do you really need this. If so, loading data from a partial is not what I consider a good practice. You should load data in a controller, manipulate it, and pass it to the (view)model.

    To come back to your question: why don't you load data from the controller. Will be easier to load async from there.

    A second remark. Why do you mean with "to get injected". Because you can override the UmbracoViewPage<> and add a constructor to allow dependency injection in your ViewPage. This then allows you to get the necessary values from your view.

    Hope it helps a bit.

    Kind regards
    Damiaan

  • Josh Evenson 10 posts 113 karma points
    Apr 07, 2017 @ 13:38
    Josh Evenson
    0

    The configuration comes from a WebApi, which is a network call, so that is why I'd like to to be async. The config contains properties that need to be used in the layout file that can change (phone number, contact info, etc).

    I'd like to load this data in the controller, I understand that's the best way, but I haven't found a way to load this data for every route in the controller. I've tried setting up a custom base controller, but the OnActionExecuting method is not async.

    Otherwise, I would have to hijack every route in Umbraco manually to load this config, and we need to be able to stand up pages dynamically.

    By injected, I mean that the config just needs to be added to the view. I already have IoC setup and working.

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Apr 07, 2017 @ 13:48
    Damiaan
    0

    If IoC works, extending the ViewPage is an easy way to inject something in your view.

    You can replace the default controller easily.

    What you could do is create a Html.Action (which is also a controller) and can be loaded from the master template with @Html.Action("...")

  • Josh Evenson 10 posts 113 karma points
    Apr 07, 2017 @ 13:53
    Josh Evenson
    0

    You can't load @Html.Action from the view asynchronously. This is the exception you get when I've tried that.

    HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.

    I have replaced the default controller, yes that is easy. But I can't find a way to use that new default controller to run an action on every request.

Please Sign in or register to post replies

Write your reply to:

Draft