【问题标题】:MVC Razor page not working on IISMVC Razor 页面在 IIS 上不起作用
【发布时间】:2014-12-09 16:03:26
【问题描述】:

我有一个使用 Asp.Net MVC 开发的项目。我使用表单和 razor 视图引擎,但 razor 页面 (.cshtml) 在 IIS (IIS 7) 上不起作用。同时检查所有设置(例如处理程序映射, runAllManagedModulesForAllRequests,webpages:Enabled, pages:Version) 一切似乎都正常。当我通过提琴手检查错误但 IIS 日志看起来很干净时,将 "500 Internal Server Error" 作为错误返回。 什么可能导致此错误?

【问题讨论】:

  • 抛出了什么异常?如果是 .NET 错误,您应该会看到一个黄屏,它会为您提供一些有用的信息。
  • 另外,检查 Windows 事件日志,尤其是应用程序日志。
  • 不返回任何错误指定的异常,剃须刀页面呈现为空(如白页)。我在通过 Fiddler 检查时遇到了这个异常。
  • @JoeEnos:不,一旦部署(IIS 7)就会发生这种情况。不过,OP 可以通过在 Web.config 的 <system.web> 部分临时添加 <customErrors mode="Off" /> 来启用此功能。
  • 我尝试了几个解决方案,我将编写异常详细信息。实际上剃须刀页面正常工作它突然坏了。

标签: asp.net asp.net-mvc asp.net-mvc-4 razor iis-7


【解决方案1】:

一种可能性是最近发布的 Windows 安全更新MS14-059

基本上,该更新完全卸载了 MVC 4.0.0.0 并在您的服务器上将其替换为 4.0.0.1,这让许多构建损坏的人感到悲痛。解决方法通常是将相关的 assemblyBinding 重定向添加到 Web.config,但还有其他解决方案 - check here 了解详情。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.1" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

【讨论】:

    【解决方案2】:

    确保您的 IIS Web 池使用与您的应用相同的 .net 版本。

    【讨论】:

      【解决方案3】:

      问题在于 razor 视图引擎版本。我不得不将表单页面 (.aspx) 替换为现有页面。我已经解决了我的问题,但这不是正确的解决方法。

      问题详情:

      [A]System.Web.WebPages.Razor.Configuration.HostSection 无法转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。类型 A 源自位置 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System .Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。类型 B 源自位置 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System .Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。

      【讨论】:

        【解决方案4】:

        这是我的 cshtml 可以正常工作,包括剃须刀和引导程序

         @using System.Data
         @model IEnumerable<CampRoll.Models.Camp>
        
        <div class="container">
        
        <h2>@ViewBag.PageTitle</h2>
        <p>
            @Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-success" })
        </p>
        <div class="row">
            <div class="col-xs-8">
                <table id='campsTable' class="table table-condensed table-striped">
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.Title)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Leader)
                        </th>
                        <th>
                            @*@Html.ActionLink("StartDate",null,new{sortOrder=@ViewBag.dateOrder},
                                new{@class="btn btn-info btn-xs"})*@
                            <a class="btn btn-xs btn-info" href="@Url.Action("Index", new { sortOrder = @ViewBag.dateOrder })">
                                @if (ViewBag.dateOrder == "ascDate")
                                {
                                    <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
                                }
                                else if (ViewBag.dateOrder == "descDate")
                                {
                                    <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
        
                                }
                                &nbsp;StartDate
                            </a>
                        </th>
                        <th><a class="btn btn-xs btn-info" href="@Url.Action("Index", new { sortOrder = @ViewBag.numberOrder })">
                            @if (ViewBag.numberOrder == "descNumber")
                            {
                                <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                            }
                            else if (ViewBag.numberOrder == "ascNumber")
                            {
                                <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
        
                            }
                            &nbsp;Number
                        </a></th>
                        <th></th>
                    </tr>
        
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                <span class="btn btn-xs btn-warning" 
                                      onclick="showChildren('@item.CampId')">@Html.DisplayFor(modelItem => item.Title)</span>
                            </td>
                            <td>
                                <span class="btn btn-xs btn-danger" onclick="showLeader('@item.LeaderId')">
                                    @Html.DisplayFor(modelItem => item.Leader.FirstName)
                                </span>
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.StartDate)
                            </td>
                            <td>
                                @if (item.Children.Count == 0)
                                {
                                    <p style="margin: 0px">None</p>
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => item.Children.Count)
                                }
        
                            </td>
                            <td>
                                @Html.ActionLink("Edit", "Edit", new { id = item.CampId }, new { @class = "btn btn-info btn-xs" })
                                @Html.ActionLink("Details", "Details", new { id = item.CampId }, new { @class = "btn btn-info btn-xs" })
                                @Html.ActionLink("Delete", "Delete", new { id = item.CampId }, new { @class = "btn btn-danger btn-xs" })
                            </td>
                        </tr>
                    }
        
                </table>
            </div>
            <div class="col-xs-4">
                <div id="Detail"></div>
                <div>
                    <form id="CreateChild" hidden="">
                        <div class="form-group" style="margin-top: 10px">
                            <input type="hidden" name="campId">
                            <input type="submit" value="Add" class="btn btn-xs btn-success" style="margin-top: -5px;" />
                            &nbsp;<input type="text" name="name"><span>&nbsp;to Camp</span>
                        </div>
                    </form>
                    <form id="EditChild" hidden="">
                        <div class="form-group" style="margin-top: 10px">
                            <input type="hidden" name="campId"><input type="hidden" name="childId">
                            <input type="hidden" name="sex">
                            <input type="submit" value="Save" class="btn btn-xs btn-success" style="margin-top: -5px;" />
                            <input type="button" onclick="$('#EditChild').hide(); $('#CreateChild').show();" value="Cancel" class="btn btn-xs btn-warning" style="margin-top: -5px;" />
                            <input type="text" name="name" value="">
                            <div style="margin:-5px 5px" class='badge alert-info'><input type="radio" name="male" value="">M
                            <input type="radio" name="female" value="">F</div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- End Details -->
        </div>
        

          @section scripts
          {
        <script>
            $(function() { // ready event
                toastr.info('Welcome to Camp Roll');
                // Handle toggle of male/female
                $('#EditChild :input[name="male"]').click(function() {
                    $('#EditChild :input[name="female"]').prop('checked', false);
                });
                $('#EditChild :input[name="female"]').click(function() {
                    $('#EditChild :input[name="male"]').prop('checked', false);
                });
                @if (Model.Any())
                {
                    // On display show the children in the first camp
                    <text>
                        showChildren(@Model.First().CampId);
                    </text>
                }
            });
        
            // POSTback of creating a new child
            $('#CreateChild').submit(function() {
                if ($(this).find('input[name="name"]').val()!="") {
                    $.ajax({
                        url: '@Url.Action("CreateChild")',
                        type: "POST",
                        data: $(this).serialize(), // serialize the input controls for this form '#CreateChild'
                        success: function() {
                            showChildren($('form').find('input[name="campId"]').val()); // refresh the table of children
                            toastr.info($('form').find('input[name="name"]').val() + ' Added'); // toast
                            $('form').find('input[name="name"]').val(""); // zero out the add child box
                        }
                    });
                }
                return false;
            });
        
            // Display leader of the camp
            function showLeader(leaderId) {
                // When Leader name clicked, invoke Controler action method to return partial view and ajax it into place
                @*$('#Detail').load('@Url.Action("LeaderById")' + '?id=' + leaderId).fadeIn(1000);*@
                $.ajax({
                        url: '@Url.Action("LeaderById")',
                        data: { id: leaderId },
                        success: function(data) {
                            $('#Detail').hide();
                            $('#CreateChild').hide();
                            $('#Detail').html(data);
                            $('#Detail').fadeIn(1000);
                        },
                        error: function() {
                            $('#Detail').html("<h3>Couldn't find a leader</h3>");
                        }
                    }
                );
            }
        
            // ajax Display of Children in the selected camp
            function showChildren(campId) {
                // When Leader name clicked, invoke Controler action method to return partial view and ajax it into place
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("ChildrenById")',
                    data: { id: campId },
                    success: function(data) {
                        $('#Detail').hide();
                        $('#CreateChild').hide();
                        $('#Detail').html(data);
                        $('#Detail').fadeIn("slow");
                        $('#CreateChild').find('input[name="campId"]').val(campId); // set campId for the Create Child form
                        $('#CreateChild').find('input[name="name"]').val("");
                        $('#CreateChild').fadeIn("slow");
                    },
                    error: function(data) {
                        $('#Detail').html('<h3>Error in retrieval</h3>');
                    }
                });
            }
        
            function showEditChild(campId, childId, childName, sex) {
                $('#EditChild').slideDown("fast");
                $('#EditChild').show();
                $('#CreateChild').hide();
                $('#EditChild :input[name="childId"]').val(childId);
                $('#EditChild :input[name="campId"]').val(campId);
                $('#EditChild :input[name="name"]').val(childName);
                if (sex == 'Male') {
                    $('#EditChild :input[name="male"]').prop('checked', true);
                    $('#EditChild :input[name="female"]').prop('checked', false);
                } else {
                    $('#EditChild :input[name="female"]').prop('checked', true);
                    $('#EditChild :input[name="male"]').prop('checked', false);
                }
            }
        
            $('#EditChild').submit(function() {
                if ($('#EditChild :input[name="male"]').prop('checked') == true) {
                    $('#EditChild :input[name="sex"]').val('male');
                } else {
                    $('#EditChild :input[name="sex"]').val('female');
                }
                $.ajax({
                    url: '@Url.Action("UpdateChild")',
                    type: "POST",
                    data: $(this).serialize(),
                    success: function() {
                        $('#EditChild').hide();
                        showChildren($('#EditChild :input[name="campId"]').val()); // refresh the table of children
                        toastr.info($('#EditChild :input[name="name"]').val() + ' Updated');
                    },
                    error: function(data) {
                        $('#Detail').html('<h3>Could not update Child record</h3>');
                    }
                });
                return false;
            });
        
            function deleteChild(childId, campId) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("DeleteChild")',
                    data: { id: childId, campId: campId },
                    success: function(data) {
                        $('#Detail').hide();
                        $('#CreateChild').hide();
                        $('#Detail').html(data);
                        $('#Detail').fadeIn("slow");
                        $('#CreateChild').find('input[name="campId"]').val(campId); // set campId for the Create Child form
                        $('#CreateChild').find('input[name="name"]').val("");
                        $('#CreateChild').fadeIn("slow");
                    },
                    error: function(data) {
                        $('#Detail').html('<h3>Error in retrieval</h3>');
                    }
                });
            }
        </script>
        

        }

        【讨论】:

          【解决方案5】:

          删除

           @model CampRoll.Models.Camp
          
          <div class="container">
          <div class="panel panel-info">
              <div class="panel-heading">
                  <h4>Confirm deletion of @Model.Title</h4>
              </div>
          </div>
          <div class="panel-body">
          
              <fieldset>
                  <legend>Details</legend>
          
                  <div>
                      @Html.DisplayFor(model => model.Title) starting&nbsp;
                      @Html.DisplayFor(model => model.StartDate)
                      @using (Html.BeginForm())
                      {
                          @Html.AntiForgeryToken()
                              <input type="submit" value="Delete" class="btn btn-danger" />
                      }
                  </div>
              </fieldset>
          
          </div>
          <div class="panel-footer">
              @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-success" })
          </div>
          

          【讨论】:

            【解决方案6】:

            创建 @使用 System.Web.UI.WebControls @使用 CA2.Models @model CA2.Models.Movie

            <div class="container">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <div class="lead">Create Movie</div>
                </div>
                <div class="panel-body">
                    @using (Html.BeginForm())
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)
            
                        <div class="row">
                            <div class="form-group col-xs-4">
                                @Html.LabelFor(model => model.MovieTitle)
                                @Html.TextBoxFor(model => model.MovieTitle, new { @class = "form-control" })
                                @*@Html.ValidationMessageFor(model => model.MovieTitle)*@
                            </div>
                            <div class="form-group col-xs-4">
                                @Html.LabelFor(model => model.Director)
                                @*@Html.TextBoxFor(model => model.StartDate,new { @class = "form-control" })*@
                                <div class='input-group' id='datetimepicker1'>
                                    <input type='text' name="MovieTitle" />
                                    <span class="input-group-addon"></span>
                                </div>
                                @*@Html.ValidationMessageFor(model => model.Director)*@
                            </div>
                            @*<div class="col-xs-4">
                                @Html.LabelFor(model => model.Actors)
                                @Html.DropDownList("Actors",
                                    new SelectList(ViewBag.Actors, "MovieTitle"),
                                    new { @class = "form-control" })
                            </div>*@
                        </div>
                        <div>
                            <button class="btn btn-success"><span class="glyphicon glyphicon-plus"></span>Create</button>
                        </div>
                    }
                </div>
                <div class="panel-footer">
                    <a class="btn btn-danger" href="@Url.Action("Index")">
                        <span class="glyphicon glyphicon-home"></span>&nbsp;Back to List
                    </a>
                    @*@Html.ActionLink("Back to List", "Index",null,new {@class="btn btn-xs btn-info"})*@
                </div>
            </div>
            

            【讨论】:

              【解决方案7】:

              编辑

              @using CA2.Models
              @model CA2.Models.Movie
              
              <div class="container">
              <div class="panel panel-info">
                  <div class="panel-heading">
                      <h4>Edit @Model</h4>
                  </div>
              </div>
              <div class="panel-body">
                  @using (Html.BeginForm())
                  {
                      @Html.AntiForgeryToken()
                      @Html.ValidationSummary(true)
                      <fieldset>
                          <legend>Movie</legend>
                          @Html.HiddenFor(model => model.Id)
                          <div class="row">
                              <div class="form-group col-xs-4">
                                  @Html.LabelFor(model => model.MovieTitle)
                                  @Html.TextBoxFor(model => model.MovieTitle,
                                          new { @class = "form-control" })
                                  @Html.ValidationMessageFor(model => model.MovieTitle)
                              </div>
                              <div class="form-group col-xs-4">
                                  @*@Html.LabelFor(model => model.Id)
                                  <div class='input-group date' id='datetimepicker1'>
                                      <input type='text' name="StartDate" value="@Model.Director" class="form-control" data-date-format="DD/MM/YYYY" />
                                      <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                  </div>*@
                                  @*@Html.ValidationMessageFor(model => model.Director)*@
                              </div>
              
                              <div class="form-group col-xs-4">
                                  @*@Html.LabelFor(model => model.Actors)
                                  @Html.DropDownList("Actors", new SelectList(ViewBag.Actors, "Actors", Model.Actors),
                                      new { @class = "form-control" })*@
                              </div>
                          </div>
                          <div class="form-group">
                              <input type="submit" class="btn btn-primary"
                                     value="Save" />
                          </div>
                      </fieldset>
                  }
              </div>
              <div class="panel-footer">
                  @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-success" })
              </div>
              

              【讨论】:

                猜你喜欢
                • 2021-01-03
                • 2021-02-13
                • 2015-08-05
                • 2019-12-16
                • 2017-03-15
                • 2021-01-17
                • 2017-06-03
                • 2010-11-03
                • 2019-02-04
                相关资源
                最近更新 更多