0

This might be a bit of a simple question. I have implemented a scaffolded controller for a Web API but can't seem to find the right url to hit it in a browser.

The controller is called ComputerAddController.cs

I've tried:

http://localhost:port/api/ComputerAdd/2
http://localhost:port/api/ComputerAdd
http://localhost:port/ComputerAdd/api/2
http://localhost:port/ComputerAdd/api

Any direction would be much appreciated (the DB/Model has definitely got an existing entry at id 2)

My Global.asax.cs

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

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

WebApiConfig.cs

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

namespace Inbound
{
    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 }
            );
        }
    }
}

The controller:

public class ComputerAddController : ApiController
    {
        private InboundModel db = new InboundModel();

        // GET: api/ComputerAdd
        public IQueryable<tbl_computerinfo_staging> Gettbl_computerinfo_staging()
        {
            return db.tbl_computerinfo_staging;
        }

        // GET: api/ComputerAdd/5
        [ResponseType(typeof(tbl_computerinfo_staging))]
        public IHttpActionResult Gettbl_computerinfo_staging(int id)
        {
            tbl_computerinfo_staging tbl_computerinfo_staging = db.tbl_computerinfo_staging.Find(id);
            if (tbl_computerinfo_staging == null)
            {
                return NotFound();
            }

            return Ok(tbl_computerinfo_staging);
        }
2
  • Specify [HttpGet] above the controller method. Commented Nov 12, 2014 at 14:27
  • @Kumar, it has nothing to do with that. Commented Nov 12, 2014 at 21:01

1 Answer 1

1

Sorted this. Apparently in the global.asax.cs, the WebApiConfig.Register needs to be placed above the RouteConfig.Register routes:

global.asax.cs

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

    namespace Inbound
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                GlobalConfiguration.Configure(WebApiConfig.Register);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.