【问题标题】:MVC EF6 Async deleteMVC EF6 异步删除
【发布时间】:2017-02-23 18:06:27
【问题描述】:

在我的控制器中我有行动

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
    clsHost HostDAL = new clsHost();
    vw_Host vw_host = await HostDAL.GetByIdAsync(id);
    string actionStatus = HostDAL.Delete(vw_host);

    TempData["msgHost"] = actionStatus;
    return RedirectToAction("Display");
}

删除方法:

public string Delete(vw_Host host)
{
    ObjectParameter executionStatus = new ObjectParameter("ExecutionStatus", "");

    try
    {
        using (Entities context = new Entities())
        {
            string name = HttpContext.Current.User.Identity.Name.ToString();
            context.sp_Host_Delete(host.ID, HttpContext.Current.User.Identity.Name.ToString(), executionStatus);
            context.SaveChanges();
        }
    }
    catch (Exception ex)
    {
        using (Entities context = new Entities())
        {
            context.sp_LogError(this.GetType().Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.Message, HttpContext.Current.User.Identity.Name);
            context.SaveChanges();
        }

        executionStatus.Value = "Error occured. Please contact to Administrator";
    }

    return executionStatus.Value.ToString();
}

我的问题是,当我使用 Async DeleteConfirmed 操作时,在 Delete 方法中出现错误:

对象引用未设置为对象的实例。

HttpContext.Current.User.Identity.Name

另一方面,当我使用同步操作时:

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    clsHost HostDAL = new clsHost();
    vw_Host vw_host = HostDAL.GetById(id);
    string actionStatus = HostDAL.Delete(vw_host);

    TempData["msgHost"] = actionStatus;
    return RedirectToAction("Display");
}

一切正常,HttpContext.Current.User.Identity.Name 没有返回错误。 仅删除操作会出现此问题。它适用于编辑操作(甚至是异步操作)。

【问题讨论】:

  • 首先,确保HttpContext.User 确实不是null。那就看stackoverflow.com/questions/28427675/…
  • sp_Host_Delete 是存储过程吗? sp 不支持异步。
  • 您还应该考虑在异步调用之前获取用户并将其作为参数传递给 delete 方法
  • @haim770 - 它为空,我想知道为什么异步操作会发生这种情况。
  • @Salar - 它的存储过程,但即使我在达到存储过程之前设置了一个变量,它也会返回 null。我对 Edit 方法有确切的操作,它工作正常 - 即使它是异步的。

标签: c# asp.net-mvc


【解决方案1】:

您还可以考虑在调用 async 之前获取用户并将其作为参数传递给 delete 方法。

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id) {
    var name = "Unknown";
    try {
        name = HttpContext.Current.User.Identity.Name.ToString();
    }catch { }
    clsHost HostDAL = new clsHost();
    vw_Host vw_host = await HostDAL.GetByIdAsync(id);
    string actionStatus = HostDAL.Delete(vw_host, name);

    TempData["msgHost"] = actionStatus;
    return RedirectToAction("Display");
}

重构的删除方法

public string Delete(vw_Host host, string name) {
    ObjectParameter executionStatus = new ObjectParameter("ExecutionStatus", "");

    try {
        using (Entities context = new Entities()) {
            context.sp_Host_Delete(host.ID, name, executionStatus);
            context.SaveChanges();
        }
    } catch (Exception ex) {
        using (Entities context = new Entities()) {
            context.sp_LogError(this.GetType().Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.Message, name);
            context.SaveChanges();
        }

        executionStatus.Value = "Error occured. Please contact to Administrator";
    }

    return executionStatus.Value.ToString();
}

【讨论】:

  • @haim770 在 cmets 中提供的链接确实提供了一些行为细节 stackoverflow.com/questions/28427675/…
  • 我检查过,当我将 HttpContext.Current.User.Identity.Name.ToString() 移动到控制器操作时,它仍然为空。
  • 我必须添加字符串 actionedBy = System.Web.HttpContext.Current.User.Identity.Name;在 DeleteConfirmed 操作的第一项,它起作用了。文我把它放在 vw_Host vw_host = await HostDAL.GetByIdAsync(id);操作它返回错误。
  • 但这就是答案中的建议。
  • 我不知道这是否重要。
猜你喜欢
  • 1970-01-01
  • 2017-09-11
  • 2014-07-14
  • 2022-11-17
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多