【问题标题】:ASP.NET MVC 3.0 -- Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'ASP.NET MVC 3.0 - 为处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”执行子请求时出错
【发布时间】:2016-08-30 02:58:09
【问题描述】:

我正在使用 ASP.NET MVC 3.0 并在 *_Shared\Layout.cshtml* 中收到以下错误

执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。

@{ Html.RenderAction("Menu", "Nav"); } 出现错误

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>

<body>
    <div id = "header">
        <div class = "title">SPORTS STORE</div>
    </div>

    <div id = "categories">


      @{ Html.RenderAction("Menu", "Nav"); }
    </div>

    <div id = "content">
        @RenderBody()
    </div>

</body>
</html>

在控制器\NavController 中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class NavController : Controller
    {
        private IProductRepository repository;

        public NavController(IProductRepository repo)
        {
            repository = repo;
        }


        public PartialViewResult Menu()
        {

            IEnumerable<string> categories = repository.Products
                                    .Select(x => x.Category)
                                    .Distinct()
                                    .OrderBy(x => x);

            return PartialView(categories);
        }

    }
}

在视图\Nav\Menu.cshtml 中:

@model IEnumerable<string>

@
{
    Layout = null;
}

@Html.ActionLink("Home", "List", "Product")

@foreach (var link in Model)
{
    @Html.RouteLink(link, new
        {
            controller = "Product",
            action = "List",
            category = link,
            page = 1
        }
    )
}   

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我可以让这个例子起作用。

    但是,发布的代码存在问题。请注意示例中的换行符:

    @ 
    { 
        Layout = null; 
    } 
    

    虽然它实际上应该是

    @{ 
        Layout = null; 
    } 
    

    它会生成您引用的错误“执行处理程序'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错”,这无济于事,但是当我按下F5时,我被带到了一个描述更好的页面:

    解析器错误描述:在解析一个 服务此请求所需的资源。请查看以下内容 特定的解析错误详细信息并修改您的源文件 适当的。

    解析器错误消息:在 “@“ 特点。只有有效的标识符、关键字、cmets、"(" 和 “{”在代码块的开头有效,它们必须出现 紧跟在“@”之后,中间没有空格。

    【讨论】:

      【解决方案2】:

      在Menu 操作上使用[ChildActionOnly] 属性,如下所示:

      [ChildActionOnly] 
      public PartialViewResult Menu()
      {
          IEnumerable<string> categories = repository.Products
                                      .Select(x => x.Category)
                                      .Distinct()
                                      .OrderBy(x => x);
      
          return PartialView(categories);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多