简体   繁体   English

您能否从 Web API POST 读取数据 model 中不存在的其他数据字符串?

[英]Can you read additional data strings from Web API POST which are not in the data model?

In Entity Framework I have a DBGeography object:在实体框架中我有一个 DBGeography object:

using System.Data.Entity.Spatial;
...
public DbGeography Location { get; set; }

As I haven't discovered a better way to send a POINT object over JSON, I'm intending to send 'lat' and 'lng' string values from my Javascript code and use them on the server during the POST method:由于我还没有发现通过 JSON 发送POINT object 的更好方法,我打算从我的 Javascript 代码发送“lat”和“lng”字符串值,并在 POST 方法期间在服务器上使用它们:

来自 POSTMan REST 客户端的屏幕截图

public async Task<IHttpActionResult> PostI4Item(myEntity item)
        {
          var lat = (VALUE TO BE SOURCED);
          var lng = (VALUE TO BE SOURCED);
          string Point = String.Format("POINT({0} {1})", lat, lng);
          item.Location = DbGeography.PointFromText(Point, 4326);
          myEntity current = await InsertAsync(item);
          return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

The problem is I don't know how to retrieve values sent to POST that are not expected within the data model. item.Location is accessible because it has been declared.问题是我不知道如何检索发送到 POST 的值,这些值在数据 model 中不是预期的item.Location是可访问的,因为它已被声明。 But, even if I am sending extra key/value pairs in the body of 'lat' and 'lng', how can I discover those values in code ?但是,即使我在“lat”和“lng”的正文中发送额外的键/值对,我如何才能在代码中发现这些值 I can't use item.lat and item.lng as they are not declared, and I don't want extra DB columns for those values... I only want them accepted as input, but not saved.我不能使用item.latitem.lng ,因为它们没有声明,而且我不希望这些值有额外的数据库列......我只希望它们被接受为输入,但不保存。

Is there a method in the data model where I can declare a value (perhaps something read-only or internal) which is expected on the request and available for use within the POST method, but is not actually being saved to my database?数据 model 中是否有一种方法可以声明一个值(可能是只读的或内部的),该值在请求中是预期的并且可以在 POST 方法中使用,但实际上并没有保存到我的数据库中?

This post https://stackoverflow.com/a/35982194/3026149 from Mohamed Badr & Niels R, and this post https://stackoverflow.com/a/18215561/3026149 from haim770 are what have solved my problem.这篇来自 Mohamed Badr & Niels R 的帖子 https: //stackoverflow.com/a/35982194/3026149 ,以及来自 haim770 的这篇帖子https://stackoverflow.com/a/18215561/3026149解决了我的问题。

I was unaware that there was a [NotMapped] attribute available for my data model, meaning I could expect values of lat & lng without having them create database columns in Entity Framework.我不知道有一个[NotMapped]属性可用于我的数据 model,这意味着我可以期望 lat 和 lng 的值,而无需它们在 Entity Framework 中创建数据库列。

So my class becomes:所以我的 class 变成了:

using System.Data.Entity.Spatial;
using System.ComponentModel.DataAnnotations.Schema;
...
public DbGeography Location { get; set; }
[NotMapped]
public double lng { get; set; }
[NotMapped]
public double lat { get; set; }

And my code becomes:我的代码变成:

public async Task<IHttpActionResult> PostI4Item(myEntity item)
        {
          var Point = DbGeography.FromText($"POINT({item.lng} {item.lat})");
          item.Location = Point;
          myEntity current = await InsertAsync(item);
          return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM