【问题标题】:GET http://localhost/Roles/Create 500 (Internal Server Error) [closed]GET http://localhost/Roles/Create 500(内部服务器错误)[关闭]
【发布时间】:2017-06-05 23:36:55
【问题描述】:

我真的不明白为什么每当我的 RolesController 将逻辑呈现到其各自的视图时,我总是收到 Http 错误 500。这是针对 MVC5 应用程序的。

请在下面找到:

RolesController.cs

        [Route("/Roles")]
        public ActionResult Index()
        {
            if (TempData["StatusMessage"] != null)
            {
                ViewBag.StatusMessage = TempData["StatusMessage"].ToString();
            }
            else
            {
                ViewBag.StatusMessage = "";
            }

            var roles = db.Roles.ToList();
            return View("Index", roles);
        }

        // GET: /Roles/Create
        public ActionResult Create()
        {
          return View();
        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Name,Description")] ApplicationRole model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var role = new ApplicationRole()
                    {
                        Name = model.Name,
                        Description = model.Description
                    };

                    var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
                    var result = await roleManager.CreateAsync(role);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Index", "Roles");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

查看:

@model User_Manager_Interface.Models.ApplicationRole
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<div class="contenttitle">
    <h2 class="form"><span>Create A New Role</span></h2>
</div>

@using (Html.BeginForm("Create", "Roles", FormMethod.Post, new { @class = "stdform", role = "form" }))
{
    @Html.AntiForgeryToken()

    @Html.ValidationSummary()

    <p>
        <label>@Html.LabelFor(model => model.Name)</label>
        <span class="field">
            <input type="text" name="name" id="name" class="smallinput" />
        </span>
        @Html.ValidationMessageFor(model => model.Name)
        <small class="desc">Name of the role.</small>
    </p>

    <p>
        <label>@Html.LabelFor(model => model.Description)</label>
        <span class="field">
            <input type="text" name="description" id="description" class="longinput" />
        </span>
        @Html.ValidationMessageFor(model => model.Description)
        <small class="desc">A short description for the role.</small>
    </p>

    <br clear="all" /><br />

    <p class="stdformbutton">
        <button class="submit radius2">Create</button>
        <input type="reset" class="reset radius2" value="Reset Form" />
    </p>
}

@section Scripts {
    @Scripts.Render("~/bundles/forms")
}

型号:

 public class ApplicationRole : IdentityRole
    {
        [Display(Name = "Description")]
        [StringLength(100, MinimumLength = 5)]
        public string Description { get; set; }
    }

是编译器不知道在运行时要渲染什么视图的问题吗?我可能做错了什么?

更新:

这是我的响应标头:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTGluZGFcVXNlck1hbmFnZXJcRlNLVXNlck1hbmFnZXJcRlNLX1VzZXJNYW5hZ2VyX1dlYlxSb2xlc1xDcmVhdGU=?=
X-Powered-By: ASP.NET
Date: Mon, 05 Jun 2017 14:12:10 GMT

【问题讨论】:

  • 您能显示您的 500 响应吗?
  • 错误信息是什么?由于防火墙限制,我看不到您的图片。
  • 您需要进一步挖掘浏览器中的错误以查看实际问题所在。开发控制台应该会显示来自服务器的完整响应 - 查看网络选项卡。
  • Create:1 GET http://localhost:53648/Roles/Create 500 (Internal Server Error)@Amy
  • 必须调试异常(这就是导致 500 错误的原因)服务器端。我们无法为您做到这一点。

标签: c# asp.net-mvc


【解决方案1】:

好的。在几乎放弃之后,我偶然发现了这个 SO 帖子:

asp.net mvc 3 handleerror global filter always shows IIS status 500 page

并查看了@DarinDimitrov 的解决方案。

所以我做了以下事情:

  1. 我在 web.config 中启用了&lt;customErrors mode="On" /&gt;

  2. 我将此函数添加到我的 ErrorController.cs:

    public ActionResult Index() { throw new Exception("error"); }

  3. 确保我的 Error.cshtml 文件的内容大小超过 1KB,以便呈现视图。

这就解决了问题。结果,这也解决了我的这个待处理的 SO 帖子,可以在这里找到:Object Moved Here - RedirectToAction

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2012-02-23
    • 2022-10-04
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2012-03-16
    • 2021-09-07
    • 2019-02-16
    相关资源
    最近更新 更多