1
votes

I am developing angular js with web api.

I have this cotroller: Controller/MasterController, and thiis in my WebApi Config:

HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

I call this function from Global.asax, Application_Start event.

I call my web api from service.js like that:

var service = function ($http) {
    var _$http = $http;
    self = this;
    self.getMenuItems = function () {
        var promise = _$http({
            method: "GET",
            url: 'api/Master'
        }).success(function (data, status, headers, config) {

        }).error(function (data, status, headers, config) {

        });
        return promise;
    };

In debug mode, I saw that I reach this area. I get this error in chrome console: "Failed to load resource: the server responded with a status of 404"

and this is what he was trying to get to: http://localhost:12345/api/Master

Also, I tried to get to the web api controller directly through the browser, and I couldn't find it.

Thank You

2
What does your MasterController look like? The following should work if you use localhost:12345/api/Master using System.Web.Http; namespace WebApi.Controllers { public class MasterController : ApiController { public string Get() { return "Hello world"; } } }h3li0s
public class MasterController : ApiController { // GET api/<controller> public IEnumerable<MenuItem> Get() { //SOME FUNCTION } }Lichte
I tried what you proposed, didn't workLichte
I would bet this is a server-side configuration problem. What I would do is create a simple page with jQuery, then call the server using $.ajax() from Firebug; tweak the configuration and repeat until your call succeeds. Also: have you tried a Get(int id) method signature? (or int? since it is optional - I do not know C# well enough)Nikos Paraskevopoulos

2 Answers

0
votes

Just to be sure your API is working try the following:

Global.asax.cs:

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

MasterController:

using System.Web.Http;

namespace WebApi.Controllers
{
    public class MasterController : ApiController
    {
        public string Get()
        {
            return "Hello world";
        }
   }
}

Calling http:// localhost:[SomePort]/api/Master in your browser should result in this: "Hello world"

The configurations above are all standard when creating a new WebApi.

0
votes

After trying some new directions, I received new error details. The final error I received + its solution you can find here