【问题标题】:An error occurred while processing your request. MVC 4处理您的请求时发生错误。 MVC 4
【发布时间】:2014-06-19 07:18:22
【问题描述】:

我正在创建一个 MVC 4 应用程序,出现如下错误。

我尝试了很多东西,但我找不到问题所在。这是我的控制器源

    public ActionResult Index(string EventId)
    {

        HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["User"];

        if (cookie != null)
        {
            string Type = (cookie["Type"] == null || cookie["Type"] == "") ? null : cookie["Type"].ToString();
            string Username = (cookie["Username"] == null || cookie["Username"] == "") ? null : cookie["Username"].ToString();
            ViewBag.Message = Type;
            ViewBag.Username = Username;


            try
            {
                string ReplaceEventID = EventId.Replace('-', '/');

                ViewBag.Message = ReplaceEventID;
                IEnumerable<Job> JobListRelatedToEvent = DBContext.Jobs.Where(x => x.EventId == ReplaceEventID);
                return View(JobListRelatedToEvent);
            }
            catch
            {
                return View();
            }
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

更新: 当它在我的本地机器上运行时,它工作正常,但在我发布到服务器后,我得到了这个错误。

谁能告诉我怎么了?

【问题讨论】:

  • 我对MVC没有太多经验,但是你不是重定向到同一个Action,从而陷入循环吗?
  • 您是否尝试过调试您的操作方法?如果没有调试它并且如果您遇到任何异常,请在此处发布。
  • 我忘了提一些我会更新的东西。
  • 在我的本地机器上它工作正常,但是在我发布到服务器后发生了这个错误。
  • 我建议您在此处检索并粘贴服务器事件日志。不知道出了什么问题

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

我面临同样的问题,如果自定义错误开启,我们会遇到这个问题。

您需要在 web.config 中进行更改并添加以下代码。 因此,您将得到应用程序或代码级别的实际错误。

<system.web>
      <customErrors mode="Off" />

现在插入 IIS 的通用页面它会显示错误。

【讨论】:

    【解决方案2】:

    我不知道它是否会帮助你,但我遇到了一个问题,即 POST 操作有 [RequireHttps],但 GET 没有。这给我带来了问题。因此,请检查您对一个动词没有限制,对另一个动词没有限制,尤其是表单帖子。

    问题直到我发布到生产环境才出现,因为 DEBUG 指令删除了 [RequireHttps] 属性:)

    【讨论】:

      【解决方案3】:

      试试这个:

       public ActionResult Job(string EventId)
              {
      
                  HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["User"];
      
                  if (cookie != null)
                  {
                      string Type = (cookie["Type"] == null || cookie["Type"] == "") ? null : cookie["Type"].ToString();
                      string Username = (cookie["Username"] == null || cookie["Username"] == "") ? null : cookie["Username"].ToString();
                      ViewBag.Message = Type;
                      ViewBag.Username = Username;
      
      
                      try
                      {
                          string ReplaceEventID = EventId.Replace('-', '/');
      
                          ViewBag.Message = ReplaceEventID;
                          IEnumerable<Job> JobListRelatedToEvent = DBContext.Jobs.Where(x => x.EventId == ReplaceEventID);
                          return View(JobListRelatedToEvent);
                      }
                      catch
                      {
                          return View();
                      }
                  }
                  else
                  {
                      return RedirectToAction("Index", "Home");
                  }
        }
      

      【讨论】:

      • 为什么?在发布答案时说明问题所在,而不仅仅是发布代码块。此外,我能看到的唯一区别是您在无法访问代码的位置添加了 return View(); 语句。
      • 请解释你的答案是做什么的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2013-07-28
      • 2020-11-14
      • 2015-12-26
      • 2013-10-21
      相关资源
      最近更新 更多