Reputation: 1458
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);
}
Upvotes: 0
Views: 118
Reputation: 1458
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);
}
}
}
Upvotes: 1