如何使用Restful API将数据放入服务器 [英] how can put data into server using Restful API
问题描述
需要帮助!
i希望使用在C#中使用MVC4创建的restFull API将数据更新到表中,并希望使用URL来更新列数据,例如
localhost / finalWebAPI / api / pligram / ahmad / 50/5
i希望将50和50保存到数据库中的表名城市中
以下代码查询设置数据
public static AndroidDBEntities2 db = new AndroidDBEntities2();
public static bool UpdateEmployee ( String id, String country, String c_ty)
{
var emp =( from c < span class =code-keyword> in db.cities
join u in db.users on c.tid equals u.tid
where u.user_name == id
// && u.tid == c.tid
选择 c).ToList();
foreach (city c in emp)
{
c.tid_countries = country;
c.city_name = c_ty;
}
return db.SaveChanges()> 0;
这里是上述型号的控制器
public class LocationController:ApiController
{
//
// 获取:/位置/
public HttpResponseMessage Put( String id ,字符串国家/地区,字符串 city)
{
var employees = locationdata.UpdateEmployee(id,country,city);
HttpResponseMessage响应= Request.CreateResponse(HttpStatusCode.OK,员工);
返回响应;
}
}
nad路线低于
< pre lang =c#> public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute( {resource} .axd / {*} PATHINFO跨度>);
routes.MapRoute(
name: 默认,
url: {controller} / {action} / {id},
默认值: new {controller = Home,action = 索引,id = UrlParameter.Optional}
);
routes.MapRoute(
name: Default1,
url: {controller} / {action} / {name} / {country} / {city}, // 带参数的网址
默认值:< span class =code-keyword> new {action = 索引,id = UrlParameter.Optional,country = UrlParameter.Optional,city = UrlParameter.Optional} // 参数默认值
);
}
当我运行我的应用程序时显示以下错误
无法找到资源。
描述:HTTP 404 。您正在查找的资源 for (或其中一个依赖项)可能已被删除,名称已更改,或 暂时不可用。请查看以下网址,并确保 拼写正确。
请求的URL:/ finalWebAPI / api / Location / ahmad / 50/50
apiconfig的代码.cs:
routes.MapRoute(
name: Default1,
url: finalWebAPI / api / {controller} / {name} / {id} / {id1},
默认值: new {action = 索引,name = UrlParameter.Optional,id = UrlParameter.Optional,id1 = UrlParameter.Optional} // 参数默认值
);
控制器代码:
public HttpResponseMessage Put( String name,< span class =code-sdkkeyword> String id, String id1)
{
// 实现你的逻辑
}
URL:/ finalWebAPI / api / Location / ahmad / 50/50
这里ahmad是名称,50是id,50是id1
注意:确保使用HTTP方法作为PUT。
need help!
i want to update data into table using using restFull API created in C# using MVC4 and wants to update columns data using URL like
localhost/finalWebAPI/api/pligram/ahmad/50/50
i want to save 50 and 50 into a table name cities in database
in below code a query to set data
public static AndroidDBEntities2 db = new AndroidDBEntities2();
public static bool UpdateEmployee(String id,String country,String c_ty)
{
var emp = (from c in db.cities
join u in db.users on c.tid equals u.tid
where u.user_name == id
//&& u.tid== c.tid
select c).ToList();
foreach (city c in emp)
{
c.tid_countries = country;
c.city_name = c_ty;
}
return db.SaveChanges()>0 ;
here is the controller of the above model
public class LocationController : ApiController
{
//
// GET: /Location/
public HttpResponseMessage Put(String id, String country, String city)
{
var employees = locationdata.UpdateEmployee(id, country, city);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, employees);
return response;
}
}
nad routes are below
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 }
);
routes.MapRoute(
name: "Default1",
url: "{controller}/{action}/{name}/{country}/{city}", // URL with parameters
defaults: new { action = "Index", id = UrlParameter.Optional, country = UrlParameter.Optional, city = UrlParameter.Optional } // Parameter defaults
);
}
when i run my application it show the below error
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /finalWebAPI/api/Location/ahmad/50/50
Code for apiconfig.cs:
routes.MapRoute( name: "Default1", url: "finalWebAPI/api/{controller}/{name}/{id}/{id1}", defaults: new { action = "Index", name = UrlParameter.Optional, id = UrlParameter.Optional, id1 = UrlParameter.Optional } // Parameter defaults );
Code for controller:
public HttpResponseMessage Put(String name, String id, String id1) { // Implement your logic }
URL: /finalWebAPI/api/Location/ahmad/50/50
Here "ahmad" is name, 50 as id, and 50 id1
Note: Make sure that you are using HTTP method as PUT.
这篇关于如何使用Restful API将数据放入服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!