我已经开始学习restful API并需要一些动力指导。谢谢 我正在尝试将数据存储到SQL服务器中,该服务器是从Android获取的URL。我正在使用在Visual Studio中使用MVC4创建的restful API,并使用实体框架。 以下URL用于获取数据
http://localhost/finalWebAPI/api/Location/ahmad%6078.000%6089.000
我希望在分割字符串的ahmad之后保存值。 以下代码是模型类
中的数据模型 public class locationdata
{
public static AndroidDBEntities2 db = new AndroidDBEntities2();
public static string locationcordinates(string name){
if (name != null) {
string[] str = name.Split('%');
string name1 = str[0];
string id1 = str[1];
string id2 = str[2];
user u = (from u1 in db.users
where u1.user_name ==name1
select u1).First();
u.lag = id1;
u.lng = id2;
db.SaveChanges() ;
}
return null;
}
这是控制器
public HttpResponseMessage Get(string id)
{
var reg1 = locationdata.locationcordinates(id);
// var employees = locationdata.UpdateEmployee(name,l);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, reg1);
return response;
}
路线在
之下public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
答案 0 :(得分:0)
请先查看以下链接:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
默认情况下,Web API路由到api / {ControllerNam}并获取HTTP方法以在控制器中选择正确的操作。 此URL(localhost / finalWebAPI / api / Location / ahmad%6078.000%6089.000)默认情况下不指向任何控制器asp.net webapi路由。你必须把它改成localhost / api / {ControllerNam} /6078.000%6089.000