【问题标题】:how can save data return by URL from android如何保存来自android的URL返回的数据
【发布时间】:2015-12-09 13:03:10
【问题描述】:

我已经开始学习 RESTful API,需要一些指导来激发动力。谢谢 我正在尝试将数据存储到由 Android 的 URL 获取的 SQL 服务器中。我正在使用在 Visual Studio 中使用 MVC4 和实体框架创建的宁静 API。 以下网址用于获取数据

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 });

【问题讨论】:

  • 你的问题到底是什么?

标签: c# api rest model-view-controller


【解决方案1】:

请先看下面的链接:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多