locked
View Component Question RRS feed

  • Question

  • User2029849071 posted

    Hello, 

    Still learning about View Components so any help would be appreciated.

    I am using a View Components to display different menus based on what department is viewed on the site.  Here is an example of my Sales Department page with its menu on the left side.

    https://imgur.com/DHJ9t8L

    My original code for the Sales Menu View Component was this based on some blogs I read in how to create a View Component:

    namespace OnTarget.ViewComponents
    {
        public class SalesMenuViewComponent : ViewComponent
        {
            public async Task<IViewComponentResult> InvokeAsync()
            {
    
                SalesMenuViewModel salesmenu = new SalesMenuViewModel();
    
                salesmenu.Title = "Sales";
    
                return View(salesmenu);
            }
        }
    }

    But I kept getting an error on build

    This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. 

    All worked fine, but I wanted to remove the error, so I changed the code to this:

    namespace OnTarget.ViewComponents
    {
        public class SalesMenuViewComponent : ViewComponent
        {
            public IViewComponentResult Invoke()
            {
    
                SalesMenuViewModel salesmenu = new SalesMenuViewModel();
    
                salesmenu.Title = "Sales";
    
                return View(salesmenu);
            }
        }
    }

    Now that I took out the public async Task<IViewComponentResult> InvokeAsync() and replaced it with public IViewComponentResult Invoke(), everything still works and I am not getting the error.  I am just want to make sure my change was okay or should I of fixed the error in another way?

    Thanks in advance for you help.

    Tim

    Thursday, July 25, 2019 10:11 PM

Answers

  • User-821857111 posted

    IF your ViewComponent doesn't make asynchronous call (as yours doesn't) then you don't need InvokeAsync. Therefore it makes sense to use the Invoke method instead. You have taken the correct approach to removing the warning about the missing await operator.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 8:05 AM

All replies

  • User-1038772411 posted

    Hello, bigmac025

    This link help for you. kindly please refer.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2

    Thanks.

    Friday, July 26, 2019 6:16 AM
  • User-821857111 posted

    IF your ViewComponent doesn't make asynchronous call (as yours doesn't) then you don't need InvokeAsync. Therefore it makes sense to use the Invoke method instead. You have taken the correct approach to removing the warning about the missing await operator.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 8:05 AM
  • User-2054057000 posted

    Asynchronous View Component that can be used to do Asynchronous works. Include the System.Net.Http package in your project (.csproj file) for making your application to make Asynchronous HTTP requests.

    <ItemGroup>
        <PackageReference Include="System.Net.Http" Version="4.3.2" />
    </ItemGroup>



    Tuesday, July 30, 2019 5:30 AM