C#/ASP.NET - Calling Web API operation via URL? :

2019-08-17 08:52发布

问题:

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"                    
            }
        );
    }



}
}

回答1:

I'm running into the same problem and I think I narrowed it down to the Accept header of HTTP. When you make a call to the API via JavaScript passing the Accept header as JSON or XML it works.

When you make the call from the browser, you are asking for different content types, therefore you get an error 500.

I still couldn't confirm it, but looks like that.



回答2:

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.