GET http://localhost/Roles/Create 500(内部服务器错误) [英] GET http://localhost/Roles/Create 500 (Internal Server Error)
问题描述
我真的不明白为什么我的RolesController将逻辑呈现到其各自的View时,我总是得到Http Error 500的原因.这是针对MVC5应用程序的.
I am really not getting why I keep getting an Http Error 500 whenever my RolesController renders logic to its respective View. This is for an MVC5 Application.
请找到以下内容:
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; }
}
是编译器不知道在运行时呈现什么视图的问题吗?我可能做错了什么?
Is it a matter of the compiler not knowing what View to render at runtime? What could I be doing wrong?
更新:
这是我的回复标头:
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
推荐答案
好的.几乎放弃之后,我偶然发现了这篇SO帖子:
Okay. After almost giving up, I stumbled across this SO post:
asp.netmvc 3 handleerror全局过滤器始终显示IIS状态500页
并查看了@DarinDimitrov的解决方案.
And looked at @DarinDimitrov 's solution.
所以我做了以下事情:
-
我在 web.config
我将此功能添加到了 ErrorController.cs:
公共ActionResult Index(){抛出新的Exception("error");}
请确保我的 Error.cshtml 文件的内容大小超过1KB,以便呈现视图.
Made sure the contents of my Error.cshtml file has more than 1KB in size in order to render the view.
这解决了问题.结果,这也解决了我的待处理SO帖子,该帖子可以在以下位置找到:对象移至此处-RedirectToAction
And that fixed the problem. And as a result, this also solved this pending SO post of mine which could be found here: Object Moved Here - RedirectToAction
这篇关于GET http://localhost/Roles/Create 500(内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!