C# 从 ObjectResult 到字符串(或获取值)
C# From ObjectResult to string (or get value)
我正在使用 IActionResult(任务)上传文件并在我的控制器中引用它。我要取回的是文件名
控制器 ->
var imageLocation = await _imageHandler.UploadImage(image);
ImageHandler ->
public async Task<IActionResult> UploadImage(IFormFile file)
{
var result = await _imageWriter.UploadImage(file);
return new ObjectResult(result);
}
我的值存储在 imageLocation 中,但我不知道如何访问它(我需要 "Value" 字符串以便将它添加到数据库中)。
我试过搜索所有内容,但每个人都在使用列表。我这里只需要一个字符串。
希望你们能帮助我。谢谢!
您可以将结果转换为所需的类型并调用 属性
控制器
var imageLocation = await _imageHandler.UploadImage(image);
var objectResult = imageLocation as ObjectResult;
var value = objectReult.Value;
或者只是将 ImageHandler.UploadImage
函数重构为 return 实际类型以避免强制转换
public async Task<ObjectResult> UploadImage(IFormFile file) {
var result = await _imageWriter.UploadImage(file);
return new ObjectResult(result);
}
并在控制器中获取预期的值
var imageLocation = await _imageHandler.UploadImage(image);
var value = imageLocation.Value;
更好的是,函数只是return所需的值
public Task<string> UploadImage(IFormFile file) {
return _imageWriter.UploadImage(file);
}
这样你就可以在控制器中调用函数时得到预期的结果。
string imageLocation = await _imageHandler.UploadImage(image);
我正在使用 IActionResult(任务)上传文件并在我的控制器中引用它。我要取回的是文件名
控制器 ->
var imageLocation = await _imageHandler.UploadImage(image);
ImageHandler ->
public async Task<IActionResult> UploadImage(IFormFile file)
{
var result = await _imageWriter.UploadImage(file);
return new ObjectResult(result);
}
我的值存储在 imageLocation 中,但我不知道如何访问它(我需要 "Value" 字符串以便将它添加到数据库中)。
我试过搜索所有内容,但每个人都在使用列表。我这里只需要一个字符串。 希望你们能帮助我。谢谢!
您可以将结果转换为所需的类型并调用 属性
控制器
var imageLocation = await _imageHandler.UploadImage(image);
var objectResult = imageLocation as ObjectResult;
var value = objectReult.Value;
或者只是将 ImageHandler.UploadImage
函数重构为 return 实际类型以避免强制转换
public async Task<ObjectResult> UploadImage(IFormFile file) {
var result = await _imageWriter.UploadImage(file);
return new ObjectResult(result);
}
并在控制器中获取预期的值
var imageLocation = await _imageHandler.UploadImage(image);
var value = imageLocation.Value;
更好的是,函数只是return所需的值
public Task<string> UploadImage(IFormFile file) {
return _imageWriter.UploadImage(file);
}
这样你就可以在控制器中调用函数时得到预期的结果。
string imageLocation = await _imageHandler.UploadImage(image);