API返回空体

时间:2016-06-06 13:42:38

标签: c# asp.net-mvc-4 unity-container

所以我有以下两种API方法:

//GET person
public IEnumerable<Person> Index()
{
    IEnumerable<Person> persons = context.Person.AsEnumerable();
    foreach(Person p in persons){
        System.Diagnostics.Debug.WriteLine(p.FirstName); //prints out all names
    }
    return persons; //returns empty body
}
//GET person/{id}
public Person Index(int id)
{
    return context.Person.Find(id);

}

这是我的要求:

GET /person HTTP/1.1
Host: localhost:55073
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4

回复:

HTTP/1.1 200 OK
Cache-Control: private
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcamVzcGVyXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTJcUHJvamVjdHNcV29ya1Rlc3RcV29ya1Rlc3RcZW1wbG95ZWU=?=
X-Powered-By: ASP.NET
Date: Mon, 06 Jun 2016 13:33:37 GMT
Content-Length: 0

为什么Content-length为0?

另外,为什么当我尝试调用第二个API方法时,为什么没有找到404资源?

这是我的RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

如果有任何不同,我正在使用带有Unity WebAPI的Asp.net MVC 4 Web API。

编辑: 似乎我以某种方式错误配置了Unity。我修复了PersonController中的错误,然后出现了此错误:

  

确保控制器具有无参数的公共构造函数。

在我的global.asax文件中我这个:

protected void Application_Start()
{
    Bootstrapper.Initialise();
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

和Bootstrapper.cs看起来像这样

public static IUnityContainer Initialise()
{
    var container = BuildUnityContainer();
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    return container;
}

private static IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer();
    container.RegisterType<IRepository<Person, int>, PersonRepository>();   
    RegisterTypes(container);

    return container;
}

根据我的阅读,您需要更改以使其正常工作吗?

0 个答案:

没有答案