c# - No HTTP resource was found that matches the request URI 'Myurl' No action was found on the controller 'controllername' that matches the request -


webapimethod work in restconsole when webapi method called in androidapplication shows error

{ "message": "no http resource found matches request uri 'http://beatrootstudios.co.in/api/apicityinfo/postsearchbycategory'.",

"messagedetail": "no action found on controller 'apicityinfo' matches request." }

even tried chaging controller name cityinfo apicityinfo , various routing tricks mentions in many examples in google

i tried putting actionname httppost verb mentioned in controller below ...

my contoller code is

public class apicityinfocontroller : apicontroller {     private dbcontext db = new dbcontext();      [httppost, actionname("postsearchbycategory")]     public ihttpactionresult postsearchbycategory(string categoryname)     {         var errormessage = "";         int errorcode = 0;         //string json = null;         string jsonresult = "error";         var subcategory = db.citydatas.where(x => x.categoryname.equals(categoryname)).tolist().firstordefault().subcategoryname;           try         {              if (subcategory !=null)             {                 //errormessage = " login id cannot null ";                 //errorcode = 1;                 var subcategoryresult = (from p in db.citydatas                               p.categoryname.equals(categoryname)                               select new                               {                                    subcategoryname = p.subcategoryname,                                 }                    ).tolist();                 var data = new                 {                     result = new                     {                         //errorcode = errorcode,                         //errormessage = errormessage,                         resultset = subcategoryresult,                      }                 };                 return json(data);              }             else             {                 var dataresult = (from p in db.citydatas                                          p.categoryname.equals(categoryname)                                          select new                                          {                                              dataimage = p.dataimage,                                              dataname = p.dataname,                                              dataaddress = p.dataaddress,                                              dataphoneno = p.dataphoneno,                                                                                          }                  ).tolist();                     var data = new                     {                         result = new                         {                             resultset = dataresult,                           }                     };                      //jsonresult = jsonconvert.serializeobject(data);                     return json(data);              }         }         catch (exception e)         {             errorcode = 401;             errormessage = e.message;             var data = new             {                 result = new                 {                     errorcode = errorcode,                     errormessage = "categoryname not exists",                  }             };              jsonresult = jsonconvert.serializeobject(data);         }          return ok(jsonresult);     } 

model part is

[table("cityinfo")]  public class citydata {     //[databasegeneratedattribute(databasegeneratedoption.identity)]      [key]     public int cityinfo_id { get; set; }     public string categoryname { get; set; }     public string subcategoryname { get; set; }     public string dataimage { get; set; }     public string dataname { get; set; }     public string dataaddress { get; set; }     public string dataphoneno { get; set; } } 

dbcontext in model

public class dbcontext : dbcontext {      public dbcontext()         : base("name=defaultconnection")     {     }      public dbset<webapi.models.citydata> citydatas { get; set; }  } 

table definition

create table [dbo].[cityinfo] ( [cityinfo_id]     int            identity (1, 1) not null, [categoryname]    nvarchar (100) null, [subcategoryname] nvarchar (100) null, [dataimage]       nvarchar (max) null, [dataname]        nvarchar (100) null, [dataaddress]     nvarchar (300) null, [dataphoneno]     nvarchar (100) null, primary key clustered ([cityinfo_id] asc) 

);

webapi config.cs file is

 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 }              name: "defaultapi",         routetemplate: "api/{controller}/{action}/{id}",         defaults: new { action = "post", id = routeparameter.optional }         );     } } 

route config.cs file is

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

global.asx file

 public class webapiapplication : system.web.httpapplication {     protected void application_start()     {         arearegistration.registerallareas();         globalconfiguration.configure(webapiconfig.register);         filterconfig.registerglobalfilters(globalfilters.filters);         routeconfig.registerroutes(routetable.routes);         bundleconfig.registerbundles(bundletable.bundles);         database.setinitializer<dbcontext>(null);      } } 

what doing wrong? how can fix error?

the parameter categorynameis non-optional, need include parameter in request. url should this: http://beatrootstudios.co.in/api/apicityinfo/postsearchbycategory?categoryname=category , want make action, opposed post.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -