首页 \ 问答 \ 不能使用await我的UserManager实例(Cannot use await with my instance of UserManager)

不能使用await我的UserManager实例(Cannot use await with my instance of UserManager)

我正在尝试使用UserManager的实例提取所选用户,以便在视图中显示有关特定用户的详细信息。 我想使用以下代码行:

var user = await _userManager.Users.SingleOrDefaultAsync(m => m.Id == id);

但是,这会产生以下错误:

“'await'运算符只能在异步方法中使用。”

我真的很困惑,因为SingleOrDefaultAsync方法肯定是一个异步方法 - 线索就在名字中! 当我将鼠标悬停在鼠标上时,它甚至会说'等待',那么为什么我不能在这种情况下使用'await'关键字呢?

我已经尝试取出'await'关键字,然后它会编译,但如果我尝试访问View,我会收到以下错误:

InvalidOperationException:传递给ViewDataDictionary的模型项的类型为'System.Threading.Tasks.Task`1 [MyProject.Models.ApplicationUser]',但此ViewDataDictionary实例需要类型为“MyProject.Models.ApplicationUser”的模型项。

对于上下文,这是我的控制器方法:

    //
    // GET /Account/Details/5
    public IActionResult Details(string id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var user = _userManager.Users.SingleOrDefaultAsync(m => m.Id == id);

        if (user == null)
        {
            return NotFound();
        }

        return View(user);
    }

这是我的观点所期待的:

@model ApplicationUser

任何帮助都非常感谢,并且如果这是一件非常容易让我苦苦挣扎的事情而道歉,我仍然是.Net和MVC的初学者!


I am trying to extract the selected user using an instance of UserManager, in order to display details about a particular user in a view. I am trying to use the following line of code:

var user = await _userManager.Users.SingleOrDefaultAsync(m => m.Id == id);

However, this gives the following error:

"The 'await' operator can only be used within an async method."

I'm really confused, because surely the SingleOrDefaultAsync method is an async method - the clue is in the name! It even says 'awaitable' when I hover my mouse over it, so why can't I use the 'await' keyword in this situation?

I've tried just taking out the 'await' keyword, and it will then compile but if I try to access the View, I get the following error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Threading.Tasks.Task`1[MyProject.Models.ApplicationUser]', but this ViewDataDictionary instance requires a model item of type 'MyProject.Models.ApplicationUser'.

For context, here is my controller method:

    //
    // GET /Account/Details/5
    public IActionResult Details(string id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var user = _userManager.Users.SingleOrDefaultAsync(m => m.Id == id);

        if (user == null)
        {
            return NotFound();
        }

        return View(user);
    }

and here is what my View is expecting:

@model ApplicationUser

Any help is much appreciated, and apologies if this is a very easy thing that I'm struggling on, I'm still very much a beginner with .Net and MVC!


原文:https://stackoverflow.com/questions/46717039
更新时间:2023-02-25 10:02

最满意答案

也许Fiddler不会自动编码空间,就像其他工具一样? 尝试:

http://localhost/MyApp/odata/People()?$filter=Name%20eq%20'John'

您也可以尝试在字符串值周围编码单引号。

另一个想法是Fiddler在使用“localhost”方面存在一些已知问题,我通常通过在请求URI中的localhost之后放置一个点(' . ')来解决这个问题:

http://localhost./MyApp/odata/People()?$filter=Name%20eq%20'John'

Maybe Fiddler isn't automatically encoding spaces, like other tools might be? Try:

http://localhost/MyApp/odata/People()?$filter=Name%20eq%20'John'

You might also try encoding the single quotes around the string value.

Another thought is that Fiddler has some known issues with using "localhost", which I usually work around by putting a dot ('.') after localhost in the request URI:

http://localhost./MyApp/odata/People()?$filter=Name%20eq%20'John'

相关问答

更多
  • Fiddler实际上通过代理工作,并在运行时自动在IE中设置代理设置。 您可以通过启动提琴手然后检查Internet选项>连接下的“局域网设置”来检查。 Invoke-RestMethod具有可用于指向Fiddler代理的-Proxy参数。 我认为默认值是127.0.0.1:8888: Invoke-RestMethod -Proxy 'http://127.0.0.1:8888' .... 编辑 :添加截图以确认上面的答案。 Fiddler actually works via a proxy, and ...
  • Fiddler没有浏览器对象模型,因此未定义XMLHttpRequest 。 幸运的是,它提供了许多不同的方式来发送HTTP请求; 例如: var oSD = new System.Collections.Specialized.StringDictionary(); var newSession = FiddlerApplication.oProxy.SendRequestAndWait(oSession.oRequest.headers, oSession.requestBodyBytes, oSD, ...
  • 也许Fiddler不会自动编码空间,就像其他工具一样? 尝试: http://localhost/MyApp/odata/People()?$filter=Name%20eq%20'John' 您也可以尝试在字符串值周围编码单引号。 另一个想法是Fiddler在使用“localhost”方面存在一些已知问题,我通常通过在请求URI中的localhost之后放置一个点(' . ')来解决这个问题: http://localhost./MyApp/odata/People()?$filter=Name%20e ...
  • 我也无法找到任何有关这方面的信息 - 它真的感觉像是OData中的一个问题。 以下是我如何设法使其发挥作用。 明确定义外键 class Student { public int TeacherId { get; set; } [Required, ForeignKey("TeacherId")] public virtual Teacher Teacher { get; set; } } 执行插入时,获取相关记录并修复模型状态: public IHtt ...
  • 插入 await Task.Delay(50); 所有请求之间修复了问题(我还没有在不同的延迟测试)。 因为Fiddler解决了这个问题,我怀疑这是一个问题,相机有太快连续发送的请求,而且小提琴手以更宽容的速度发送它们。 因为它是一个异步等待,所以没有明显的性能影响,除了花了一点时间来通过启动时通过所有~20(30个现在)请求,这对我的应用程序来说不是问题。 Inserting await Task.Delay(50); between all requests fixed the problem (I ...
  • 您必须在localhost之后添加句点(。),或者使用计算机名称作为要在浏览器中查看流量的站点的URL。 例如: http://localhost./foo.html 要么: http://mymachinename/foo.html 请参阅此处了解fiddler的已知文档问题。 You must add a period (.) after localhost or use the machines name for the URL of the site you want to view traff ...
  • 这个教程有点偏僻。 如果您定义了这样的路线: config.Routes.MapODataRoute("ODataRoute", "odata", model); 然后你需要浏览到: http://localhost:52868/odata 查看服务文档。 同样,你可以去: http://localhost:52868/odata/$metadata 查看元数据文档,和 http://localhost:52868/odata/Products 获取OData中的Products实体集。 The t ...
  • 尝试这个: var oProviderConfig = { name: 'oData', oDataServiceHost: 'http://localhost:xxx/Roots/' ,enableJSONP: false }; 您还必须从Web服务启用CORS支持。 如果您使用的是.NET
  • 更改请求标头: User-Agent: Fiddler Host: localhost Content-Length: 44 Content-Type: application/json Change the request header: User-Agent: Fiddler Host: localhost Content-Length: 44 Content-Type: application/json
  • 如果您使用的是OData客户端库,则可以使用此功能。 当您尝试从客户端更新/删除实体时,客户端会从您先前检索的有效内容中加载URL。 这些URL中没有“.fiddler”,Fiddler无法捕获它们。 例如,您想要更新Product对象。 通常你必须先查询对象。 在对象中存在以某种方式存储的编辑链接。 编辑链接是一个URL(当然没有“.fiddler”)。 稍后,当您尝试更新产品对象时,客户端会将PUT / PATCH请求发送到编辑链接。 This is expected if you are using ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)