Friday 15 March 2013

ASP.Net Web API - Find the Correct URL -



ASP.Net Web API - Find the Correct URL -

this might bit of simple question. have implemented scaffolded controller web api can't seem find right url nail in browser.

the controller 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 much appreciated (the db/model has got existing entry @ 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() { homecoming 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) { homecoming notfound(); } homecoming ok(tbl_computerinfo_staging); }

sorted this. apparently in global.asax.cs, webapiconfig.register needs placed above 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); } } }

asp.net asp.net-mvc razor asp.net-web-api entity

No comments:

Post a Comment