简体   繁体   English

ASP.NET MVC Ajax中的500内部错误

[英]500 Internal Error in ASP.NET MVC Ajax

I'm using Visual Studio 2010 and MVC 4 for my web application. 我正在为我的Web应用程序使用Visual Studio 2010和MVC 4。 This is my controller code: 这是我的控制器代码:

public ActionResult MyController() 公共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. 此方法将成功完成,但是我的ajax容器未显示任何内容。 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. 可能以多种方式显示500 Internal Server Error消息,因为在服务器上未正确处理某些内容。 In your case, as the commends, your MyContainer type does not implement IDisposable interface, so, you cannot use this type on the using(){ } block. 如您MyContainer ,您的MyContainer类型没有实现IDisposable接口,因此,您不能在using(){ }块上使用此类型。 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. 当在using块上使用一种类型时,该类型必须实现IDIsposable,因为当它继承时,.Net Framework将从堆和引用中删除该实例。 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("/");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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