0

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.

Please find the below:

enter image description here

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);
        }

View:

@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")
}

Model:

 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?

UPDATE:

Here is my response header:

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
Harold_Finch
  • 682
  • 2
  • 12
  • 33

1 Answers1

0

Okay. After almost giving up, I stumbled across this SO post:

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

And looked at @DarinDimitrov 's solution.

So I did the following:

  1. I enabled <customErrors mode="On" /> in my web.config

  2. I added this function to my ErrorController.cs:

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

  3. Made sure the contents of my Error.cshtml file has more than 1KB in size in order to render the view.

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

Harold_Finch
  • 682
  • 2
  • 12
  • 33