";System.Threading.Tasks.Task `1[System.Web.Mvc.ActionRe

本文关键字:System Web Mvc ActionRe Task quot Threading Tasks | 更新日期: 2023-09-27 18:23:36

我有一个控制器

public class InvitationController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create(InvitationModel invitationmodel)
    {
        if (ModelState.IsValid)
        {
            var regLink = _repo.SaveAndGetRegistrationLink(invitationmodel);

            IEMailer mailer = new EMailer();
            var inv = mailer.Invitation(invitationmodel.Email, regLink);
            await Task.WhenAll(new AsyncEmailSender().SendEmail(inv));
            return RedirectToAction("Index");
        }
        return View(invitationmodel);
    }
}

它在我的本地主机上运行良好(发送电子邮件后重定向到所需的页面)。我将我的网站发布到smarterasp.net

现在它显示了一个字符串而不是重定向:

System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]

原因是什么?如何解决?

Elmah没有记录任何内容。这封电子邮件实际上已经发送了。

我尝试添加MVC dll。下面的所有文件都位于我的网站dll旁边。

  • System.Web.Mvc
  • Microsoft.Web.Infrastructure
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor
  • System.Web.Helpers

编辑

主机管理员从iis:给我发了一个日志

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.AwaitTaskContinuation.<ThrowAsyncIfNecessary>b__1(Object s)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

";System.Threading.Tasks.Task `1[System.Web.Mvc.ActionRe

也许他们有某种测试版,我不确定。但是堆栈中的System.Web.LegacyAspNetSynchronizationContext建议您需要在Web.config中添加UseTaskFriendlySynchronizationContext标志。

检查以下内容:http://forums.asp.net/t/1778103.aspx/1

我也遇到过同样的问题,我发现问题是当我从4.0升级到.NET framework 4.5时,当我升级MVC 3时,它没有升级,也不会处理.NET 4.5。我发现我必须升级到MVC 4,我在NuGet上发现了Nandip Makawana的一个名为upgrade MVC 3 to MVC 4的包。当我使用这个时,它很好地解决了问题,但我又犯了一个错误。升级后,我的Web.config文件发生了更改,需要进行修复。这很容易,并在抛出的错误中进行了解释。

<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="webpages:Version" value="2.0.0.0" /> <---------- This line
<add key="PreserveLoginUrl" value="true" />

网页版本更改为2,需要更改回1

<add key="webpages:Version" value="1.0.0.0" />

经过这次更改后,我的应用程序运行得非常好。我希望这能帮助到别人,我自己花了3天时间才找到答案。