asp.net - LinqToTwitter BeginAuthorizationAsync 抛出 NullReferenceException

标签 asp.net vb.net asp.net-mvc-4 linq-to-twitter

我正在尝试使用 LinqToTwitter nuget 插件 (v3.1.2) 将 twitter 集成添加到现有的 MVC4 应用程序中,并遵循文档 here

Imports LinqToTwitter

Public Class OAuthController
Inherits System.Web.Mvc.Controller

Async Function BeginAsync() As Threading.Tasks.Task(Of ActionResult)
    Dim auth = New MvcAuthorizer() With { _
        .CredentialStore = New SessionStateCredentialStore() With { _
            .ConsumerKey = ConfigurationManager.AppSettings("consumerKey"), _
            .ConsumerSecret = ConfigurationManager.AppSettings("consumerSecret") _
        } _
    }

    Dim twitterCallbackUrl As String = Request.Url.ToString().Replace("Begin", "Complete")
    Dim callbackUrl = New Uri(twitterCallbackUrl)
    Return Await auth.BeginAuthorizationAsync(callbackUrl)
End Function

第二行到最后一行抛出一个 System.NullReferenceException:对象引用未设置到对象的实例。 异常,因为错误是从插件抛出的,我不确定如何继续。

堆栈跟踪 here

最佳答案

我发现了问题。根本原因是 BeginAuthorizationAsync 在幕后使用 HttpContext.Current,这不是访问 MVC Controller 上下文的正确方法。

我已在此处提交了拉取请求:https://github.com/JoeMayo/LinqToTwitter/pull/18

作为解决方法,您可以在调用 BeginAuthorizationAsync 之前添加以下代码。

auth.GoToTwitterAuthorization = (authUrl) => {
    ControllerContext.HttpContext.Response.Redirect(authUrl);
};

关于asp.net - LinqToTwitter BeginAuthorizationAsync 抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873612/

相关文章:

.net - List(Of T) 被未知英雄救了?

c# - Razor block 内的 asp.net mvc 4 javascript 抛出错误

c# - 找不到 OdbcConnection DataName 和 DataSource

asp.net - HTTP 500 内部错误 - IIS 网站

c# - 获取客户端机器的IP地址

c# - 如何设置Janus GridEX列索引

javascript - 在外部 javascript 文件中获取 asp 控件 ID 的最佳方法是什么?

vb.net - 组合框未填充代码

c# - 该表有一列带有复选框,需要选择该行并将行发送到另一个表。

asp.net-mvc - 从 Asp.Net MVC 3 迁移到 MVC 4 后,捆绑有效,但缩小不起作用