Use of await in Razor views

前端 未结 8 857
面向向阳花
面向向阳花 2020-11-28 12:48

Is it possible to await on tasks in Razor .cshtml views?

By default it complains that it can only be used in methods marked with async so I

相关标签:
8条回答
  • 2020-11-28 13:09

    No, that's not possible and you shouldn't need to do it anyway. Razor views should contain markup and at most some helper call. async/await belongs to your backend logic.

    0 讨论(0)
  • 2020-11-28 13:14

    Following on MaxP's answer, it's easy to return a value from that code, despite Knagis comment:

    @{
        int x = DoAsyncStuffWrapper().Result;
    }
    @functions {
        async Task<int>DoAsyncStuffWrapper() 
        {
            await DoAsyncStuff();
        }
    }
    
    0 讨论(0)
提交回复
热议问题