我正在写一个 asp.net 核心网站,显示在其他地方生成的图片。我正在其他地方轮询 webapi,我已经编写并命名为 elseware ;-)
基本上,当图像可用并上传到我的网站服务器时,我需要更新显示图像的路径。我正在从 javascript 进行轮询,也查看图像是否可用,当它可用时,我想转到 RazorPage 的 pageModel,并在其上调用一个函数,最好传递图像的名称。然后该函数应该为图像调用 elseware,并将其存储在服务器上,并返回它的本地路径。
获取以图像名称为参数的 C# 函数有些麻烦。
.cs file:
public class xxModel:PageModel {
...
public string UploadImage(string imageName) {
var image = elseware.GetImage(imageName);
string newPath = saveImage(image);
return newPath;
}
}
.cshtml file:
var pollForImage = function (imageName) {
var imagePath = @Model.UploadImage(imageName); //<== this is where it
// fails.. I cannot get
// the call to work with
// a parameter..
$("#image").attr("src", imagePath);
}
我不知道这是否是解决它的聪明方法,但我认为它应该是可行的。如果我将 ImagePath 设为类上的公共属性,我可以覆盖该属性的 get 和 set 操作。但这并不清楚。
我不想在页面上使用 OnPost/OnGet。请注意,这是 asp.net core mvc/web api v. 2.0 - (目前相当新)