C#/ASP.NET - Calling Web API operation via URL? : HTTP Error 500
NickName:praetor Ask DateTime:2012-08-01T09:02:25

C#/ASP.NET - Calling Web API operation via URL? : HTTP Error 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"                    
            }
        );
    }



}
}

Copyright Notice:Content Author:「praetor」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11751201/c-asp-net-calling-web-api-operation-via-url-http-error-500

Answers
tucaz 2012-08-01T16:22:23

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. \n\nWhen you make the call from the browser, you are asking for different content types, therefore you get an error 500.\n\nI still couldn't confirm it, but looks like that.",


More about “C#/ASP.NET - Calling Web API operation via URL? : HTTP Error 500” related questions

Is there any difference between C c; and C c = C();?

#include&lt;iostream&gt; using namespace std; class C{ private: int value; public: C(){ value = 0; cout&lt;&lt;"default constructor"&lt;&lt;endl; }

Show Detail

Rebinding C-c to C-c

I'm using Viper, and I want to change its C-c and C-g to the original emacs functions. I can rebind C-g with (define-key viper-vi-global-user-map "C-g" 'keyboard-quit), but how can I rebind C-c, si...

Show Detail

Why value of variable c in (c=c==c) is 2 instead of 1,in C programming?

#include&lt;stdio.h&gt; int main() { int a=10,b=2,c; a=!++b&amp;&amp;(c=c==c); printf("b value is %d \n",b); printf("c value is %d \n",c);

Show Detail

The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java

Let's say we have class A as a parent class, and class C that extends it. class A { void m() { System.out.println("A.m"); } } class C extends A { @Override v

Show Detail

Mounting of /cygdrive/c resolves to C:\cygdrive\c

I am using Cygwin and try to run a bash script which uses the pwd command to construct a path and then generate a directory. The problem is that the directory is created under c:\cygdrive\c rather ...

Show Detail

Prefix key `C-S-c` is echoed as `C-c`

When I press C-S-c, the echo area shows only C-c. Things like C-S- selection do work, however. I'm in Ubuntu 14.10 Utopic Unicorn in case this helps. Here's the code for the key binding (for mul...

Show Detail

What is the difference between C, C99, ANSI C and GNU C?

I have started programming practice on codechef and have been confused by the difference between C and C99. What does C mean here? Is it C89? Check the languages at the bottom of this submit. It co...

Show Detail

Is C# a superset of C?

Is C# a superset of C in anyway, like Objective-C or C++? Is there a way to compile C online with constructs such compiler flags?

Show Detail

C - Scope of C Functions

I apologize if this is a beginner's question, but after working in C for a bit, I finally would like a bit of clarification on exactly what kind of files/functions are available to a function. I

Show Detail

Why a != b != c is not equal to a != b and a != c and b != c?

I want to check if 3 values a, b , c are not equal to each other. Given that a == b == c equals to a == b and b == c and a == c, why does python give a different answer for a != b != c ? Thanks! ...

Show Detail