我真的没有理解为什么每当我的RolesController将逻辑呈现给各自的View时我都会得到Http Error 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
答案 0 :(得分:0)
好。几乎放弃后,我偶然发现了这个SO帖子:
asp.net mvc 3 handleerror global filter always shows IIS status 500 page
看了@DarinDimitrov的解决方案。
所以我做了以下事情:
我在 web.config
<customErrors mode="On" />
我将此功能添加到 ErrorController.cs:
public ActionResult Index()
{
throw new Exception("error");
}
确保我的 Error.cshtml 文件的内容大小超过1KB才能呈现视图。
这解决了这个问题。结果,这也解决了我的这个待定的SO帖子,可以在这里找到:Object Moved Here - RedirectToAction