Swift 4发布对ASP.NET HttpGet方法的请求

时间:2017-11-08 05:45:52

标签: c# ios asp.net swift

我正在尝试调用ASP.NET实体框架HttpGet方法:

 [HttpGet]
        public BookingLCPreview MarkAsAttended(string asAttended)
        {
            BookingLCPreview existing = db.Data.FirstOrDefault(u => u.qrCode == asAttended);
            existing.attendedPreview = true;
            db.SaveChanges();

            return existing;
        }

从我的快速4应用程序中如下:

func markAsAttended(jsonData: String) {

        if !jsonData.isEmpty {

            let url = URL(string: webservice + "BookingLCPreviewsAPI")!

            var request = URLRequest(url: url)
            request.httpMethod = "POST"
            let postString = "asAttended=" + jsonData
            request.httpBody = postString.data(using: .utf8)

            URLSession.shared.getAllTasks { (openTasks: [URLSessionTask]) in
                print(openTasks)
            }

            let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
                print(response!)
            })
            task.resume()
        }

    }

但它不起作用,所有这个调用都是将新项目发布到我的数据库,当我希望它调用MarkAsAttended并更新我的数据库中的项目时,当我在浏览器中调用URL时它会按预期运行.. ..更新记录,但在我的应用程序上添加了一个新项目。我究竟做错了什么?我尝试将HttpGet更改为HttpPost,但这不起作用:(

这是我的路线:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

0 个答案:

没有答案