MVC EF6异步删除

时间:2016-10-14 10:27:42

标签: c# asp.net-mvc

在我的控制器中,我有行动

[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();
}

Mi问题是当我使用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不会返回任何错误。 仅对Delete操作发生此问题。它适用于编辑操作(甚至是异步)。

1 个答案:

答案 0 :(得分: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();
}