我需要在我们的网站上跟踪电子邮件和网页。我们想要使用WebAPI,但是我很新,我发现的例子很难理解。这是我的问题:
我有一个EmailTrackerContoller,代码如下:
public class EmailTrackingController : Controller
{
[OutputCache(NoStore = true , Duration = 0)]
[HttpPost]
public ActionResult GetPixel(string email, Guid emailID) {
var client = new HttpClient();
var endpoint = "http://localhost:2640/api/EmailTracker/SaveEmail"; --path to my API
var response = await client.PostAsync(endpoint, null); --this does not work
const string clearGif1X1 = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
return new FileContentResult(Convert.FromBase64String(clearGif1X1) , "image/gif");
}
}
我还创建了一个WebAPI,它有一个叫做SaveEmail的HttpPost方法:
[HttpPost]
public HttpResponseMessage SaveEmail([FromBody]string value) { --How do I get the Email and Guid I need from here?
var a = new DL.EmailTracking();
a.Insert("<email>" , Guid.NewGuid());
return Request.CreateResponse(HttpStatusCode.Accepted , "");
}
关于此的几个问题:
答案 0 :(得分:2)
PostAsync
的第二个参数是通话的内容。
将对象序列化为包含所需值的JSON,并将其添加为内容。
var obj = new { Email = "mail@mail.com" };
HttpStringContent msg = new HttpStringContent(JsonConvert.SerializeObject(obj));
var response = await client.PostAsync(endpoint, msg);
修改接收方法以接收所需的属性。我使用类作为方法参数,但您也可以使用[FromBody]
列出所有属性。
public HttpResponseMessage SaveEmail(EmailSave model)