Webapimethod在RestConsole中运行良好但在AndroidApplication中调用Webapi方法时显示错误
{ “消息”:“找不到与请求URI'http://beatrootstudios.co.in/api/apiCityInfo/PostSearchByCategory'匹配的HTTP资源。”,
“MessageDetail”:“在控制器'apiCityInfo'上找不到与请求匹配的操作。” }
即使我尝试将我的Chaging控制器名称从CityInfo转移到apiCityInfo以及By 各种路由技巧在谷歌的许多例子中都有提及
我也试过把Actionname和httppost动词放在下面的控制器中提到......
我的控制器代码
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 be Null ";
//errorCode = 1;
var SubCategoryresult = (from p in db.CityDatas
where 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
where 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);
}
模型部分
[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
public class DBContext : DbContext
{
public DBContext()
: base("name=DefaultConnection")
{
}
public DbSet<WebApi.Models.CityData> CityDatas { get; set; }
}
表定义是
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文件是
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 }
);
}
}
路由Config.cs文件是
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文件
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);
}
}
我做错了什么?我该如何修复错误?
答案 0 :(得分:0)
参数CategoryName
是非可选参数,因此您需要将其作为参数包含在请求中。 URL应如下所示:http://beatrootstudios.co.in/api/apiCityInfo/PostSearchByCategory?CategoryName=category
您可能希望将此作为GET操作,而不是POST。