C#/ ASP.NET - 通过URL调用的Web API操作? :HTTP 500错误 [英] C#/ASP.NET - Calling Web API operation via URL? : HTTP Error 500

查看:2280
本文介绍了C#/ ASP.NET - 通过URL调用的Web API操作? :HTTP 500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个ASP.NET MVC应用4。从数据库中获取信息,并在网页上显示它在这个阶段很顺利。然后,我决定,我想开发一个Web API,以及并排侧与我的应用程序的进展情况。到目前为止好,直到我尝试使用本地主机上的URL来测试它。

当我尝试过了,我得到了一个HTTP错误500

我跑从2010年的VS应用程序,它开辟了的http://本地主机:23375 / 在我的浏览器。在本月底,我追加我的API调用,将它带到:

 的http://本地主机:23375 / API /性能/ ShowMachines

当我按下回车键,我得到的错误。为什么会这样,我该如何解决呢?

下面是相关code:

PerformanceController.cs

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Net;
使用System.Net.Http;
使用System.Web.Http;使用PerfMon.Models;命名空间PerfMon.Controllers
{
    公共类PerformanceController:ApiController
    {        PerfMonDataRepository perfRep =新PerfMonDataRepository();        // GET /性能/机
        [HTTPGET]
        公众的IQueryable<机械及GT; ShowMachines()
        {
            返回perfRep.GetAllMachines();
        }    }
}

的Global.asax.cs

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Http;
使用System.Web.Mvc;
使用System.Web.Optimization;
使用System.Web.Routing;命名空间的性能监视器
{
    //注意:有关启用IIS6 IIS7或经典模式的指令,
    //参观http://go.microsoft.com/?LinkId=9394801公共类WebApiApplication:System.Web.HttpApplication
{
    保护无效的Application_Start()
    {
        AreaRegistration.RegisterAllAreas();        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }    公共静态无效的RegisterRoutes(RouteCollection路线)
    {        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapHttpRoute(
            名称:机,
            routeTemplate:API / {控制器} / {行动},
            默认:新{
                控制器=性能,
                行动=ShowMachines
            }
        );
    }}
}


解决方案

的问题是,当我创建的.dbml文件,以及拖放表到它,自动生成的创建DataContext类的EntitySet对象重新present的外键关系。我创建了简单的班级,得到砂层组返回JSON,而不是在DataContext类,不包括EntitySet的对象。这样一来,它的工作就像一个魅力。显然EntitySets不是可序列化,因而被赋予了500错误。

I'm developing an ASP.NET MVC 4 application. Getting information from the database and displaying it on the webpage went well at this stage. Then I decided I'd like to develop a Web API as well side-by-side with my application's progress. So far so good, until I tried to test it using a URL on the local host.

When I tried it out, I got an HTTP Error 500.

I ran the application from VS 2010 and it opened up http://localhost:23375/ in my browser. At the end of this, I appended my API call, bringing it to:

http://localhost:23375/api/Performance/ShowMachines

When I hit enter, I get the error. Why is this so and how can I resolve it?

Here is the relevant code:

PerformanceController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using PerfMon.Models;

namespace PerfMon.Controllers
{
    public class PerformanceController : ApiController
    {

        PerfMonDataRepository perfRep = new PerfMonDataRepository();

        // GET /performance/machines
        [HttpGet]
        public IQueryable<Machine> ShowMachines()
        {
            return perfRep.GetAllMachines();
        }

    }
}

Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace PerfMon
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "Machines",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { 
                controller = "Performance",
                action = "ShowMachines"                    
            }
        );
    }



}
}

解决方案

The issue was that when I created the .dbml file, and dragged and dropped the table into it, the automatically generated DataContext class created EntitySet objects to represent the foreign-key relationships. I created simple classes with gets sand sets to return the JSON, rather than the classes in the DataContext, excluding the EntitySet objects. As a result, it worked like a charm. Apparently EntitySets are not serializeable, and thus were giving the 500 Error.

这篇关于C#/ ASP.NET - 通过URL调用的Web API操作? :HTTP 500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆