MVC 4 403 and 404 error

Scenario: I deployed a MVC 4 site to IIS7 server and when I would nav to the root http://domain.site.com/ it would give a 403 permission error. If I tried to go directly to the controller/action (http://domain.site.com/controller/action), I would get a 404 error.

Trying to load the page on the server, I could see the detail that the it was trying to use the staticFile handler, which is wrong!

I found an article that showed how to set the default controller in the web.config rather than the code, and by doing that I was able to see that the Newtonsoft JSON package was giving an error. This I’ve seen before so I checked my nuget packages, changed them all from net451 to net40, (my project is set to compile as 4.5 not 4.5.1).

I redeployed. Same error. The static handler is still firing rather than .net handler. I added

<modules runAllManagedModulesForAllRequests="true" />

to the

<system.webServer>
section.

Now it loads normally. Next time I load a new site and do all of this I will try adding that to the web.config first and the see if less needs to be done with updating the nuget packages.

_Layout setup for Bootstrap and kendoui

Using Bootstrap 3.0 and KendoUi together.

In the _layout page, head:

<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<link href=”~/Content/Kendo/2013.3.1316/kendo.common.min.css” rel=”stylesheet” type=”text/css” />
<link href=”~/Content/Kendo/2013.3.1316/kendo.default.min.css” rel=”stylesheet” type=”text/css” />
<link href=”~/Content/Kendo/2013.3.1316/kendo.dataviz.default.min.css” rel=”stylesheet” type=”text/css” />
<script src=”@Url.Content(“~/Scripts/kendo/2013.3.1316/jquery.min.js”)”></script>
<script src=”@Url.Content(“~/Scripts/kendo/2013.3.1316/kendo.all.min.js”)”></script>
<script src=”@Url.Content(“~/Scripts/kendo/2013.3.1316/kendo.aspnetmvc.min.js”)”></script>
<link rel=”stylesheet” href=”~/Content/Bootstrap/bootstrap.min.css”>
<link rel=”stylesheet” href=”~/Content/Bootstrap/bootstrap-theme.min.css”>
<script src=”@Url.Content(“~/Scripts/kendo.modernizr.custom.js”)”></script>
</head>

right before the closing html tag:

<script src="~/Content/Bootstrap/js/bootstrap.min.js"></script>

If using the bootstrap menu with dropdowns, also include this near the bottom:


<script type="text/javascript">
$('.dropdown-toggle').dropdown();
</script>

Edit form structure

Simple Edit form structure:

@model ViewModelName
@{
ViewBag.Title = "My title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Task @ViewBag.Title</h2>

@using (Html.BeginForm(“Edit”, “ControllerName”, FormMethod.Post, new { @role = “form”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.PropertyId)
<div class=”row form-group”>
<div class=”col-sm-4″>
@Html.LabelFor(model => model.PropertyName)
@Html.DisplayFor(model => model.PropertyName)
</div>

<div class=”col-sm-4″>
@Html.LabelFor(model => model.SortOrder, new {@class = “control-label”})
@Html.EditorFor(model => model.SortOrder)
@Html.ValidationMessageFor(model => model.SortOrder)

</div>
<div class=”col-sm-4″>
@Html.LabelFor(model => model.Active)

@Html.EditorFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active)
</div>
</div>

<fieldset>
<legend></legend>
<div class=”row form-group”>

vdiv class=”col-sm-6″>
@Html.LabelFor(model => model.Name, new {@class = “control-label”})

@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class=”col-sm-6″>
@Html.LabelFor(model => model.Description, new {@class = “control-label”})

@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)

</div>

</div>
</fieldset>

<div class=”row form-group”>
<div class=”col-sm-12″><input type=”submit” value=”Save” />
</div>

</div>

<div class=”row form-group”><div class=”col-sm-12″>
@Html.ActionLink(“Back to list”, “Index”)</div>
</div>

}

Display a javascript alert (or run script) from code behind page.

Using Response.Write from an asp.Net web form might work but has some side effects so using the .Net ClientScriptBlock will allow you to insert your javascript cleanly. Here is a sample from some real code I wrote.

 

// Any any function that runs on PageLoad
string csName = “alert”;
string js = “alert(‘Customer has abuse tickets’);”;
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(csName))
{
cs.RegisterStartupScript(this.GetType(),csName, js, true);
}