asp.net - MVC action results in 404 with "No type was found that matches the controller" invoking default action on controller works -


i inherited asp.net mvc code , tasked adding new features. complete beginner using asp.net mvc , come background of using web forms.

i added new controller (apicontroller) , added following actions it:

    // get: /api/index     public string index()     {         return "api methods";     }      // get: /api/detectionactivity     public jsonresult detectionactivity()     {         var detections = d in db.detections                          orderby dbfunctions.truncatetime(d.creationtime)                          group d dbfunctions.truncatetime(d.creationtime) g                          select new { date = g.key, count = g.count() };         viewbag.detectioncounts = detections.tolist();         return json(detections, jsonrequestbehavior.allowget);     } 

my routeconfig.cs has following registered routes.

    public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "dashboard", action = "index", id = urlparameter.optional }         ); 

this looks tutorials i've been reading it's not working , i'm missing something.

if go localhost:21574/api see output index() action, "api methods".

if go localhost:21574/api/detectionactivity throws 404 following data in response:

{     "message":"no http resource found matches request uri 'http://localhost:21574/api/detectionactivity'.",     "messagedetail":"no type found matches controller named 'detectionactivity'." } 

i'm thinking there need i'm not.

any suggestions on next?

update 1

i tried routeconfig.cs

        routes.maproute(name: "apicontroller",             url: "{controller}/{action}"         );          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "dashboard", action = "index", id = urlparameter.optional }         ); 

these results:

if go localhost:21574/api see output index() action, "api methods". same before.

if go localhost:21574/api/detectionactivity throws 404 following data in response:

{     "message":"no http resource found matches request uri 'http://localhost:21574/api/detectionactivity'.",     "messagedetail":"no type found matches controller named 'detectionactivity'." } 

same before.

if go localhost:21574/api/api/detectionactivity throws 404 data in response:

{     "message":"no http resource found matches request uri 'http://localhost:21574/api/api/detectionactivity'.",     "messagedetail":"no type found matches controller named 'api'." } 

now it's saying can't find controller named "api".

from route config url should be: localhost:21574/api/dashboard/detectionactivity

or if need localhost:21574/api/detectionactivity (not recommended)

change register method in webapiconfig class to

config.routes.maphttproute(         name: "defaultapi",         routetemplate: "api/{action}/{id}",         defaults: new { controller = "dashboard", action = "index", id = routeparameter.optional }     ); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -