简体   繁体   中英

500 Internal Error in ASP.NET MVC Ajax

I'm using Visual Studio 2010 and MVC 4 for my web application. This is my controller code:

public ActionResult MyController()

  { if (Request.IsAjaxRequest()) { using (MyContainer context = new MyContainer()) { try { var result = Some Query; return PartialView("_MyView", result); } catch (Exception ex) { } } } if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Home", new { area = "User" }); } else { return Redirect("/"); } } 

This method will be done successfully, But my ajax container not showing any things. In firebug this error raised:

NetworkError: 500 Internal Server Error + http://localhost....?X-Requested-With=XMLHttpRequest

Why does this error occur?
What do I do to solve this problem?
Thanks in advance!

The 500 Internal Server Error message might be seen in any number of ways because something was not processed fine on the server. In your case, as the commends, your MyContainer type does not implement IDisposable interface, so, you cannot use this type on the using(){ } block. When you use a type on a using block, this type have to implement IDIsposable because when it get over, the .Net Framework will remove the instance from the heap and the reference. I did some changes on your code without using block. Take a look:

public ActionResult ActionName()
{
    if (Request.IsAjaxRequest())
    {
        try
        {
            MyContainer context = new MyContainer();

            var result = Some Query;
            return PartialView("_MyView", result);      
        }
        catch (Exception ex)
        {
            // return some partial error that shows some message error
            return PartialView("_Error");
        }
    }

    if (User.Identity.IsAuthenticated)
    {
        return RedirectToAction("Index", "Home", new { area = "User" });
    }

    return Redirect("/");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM