我正在尝试使用 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/