从模型传回错误信息的最佳方法?

时间:2016-12-14 09:53:48

标签: asp.net-mvc model-view-controller return

我有一个从我的控制器调用上传图像的服务。这很有效,但是当不符合条件时,它当前返回'null',这是无用的或优雅的。目前的代码是:

    public async Task<string> UploadPropertyImageAsync(HttpPostedFileBase imageToUpload)
    {
        string imageFullPath = null;
        if (imageToUpload == null || imageToUpload.ContentLength == 0 || imageToUpload.ContentLength >= 8388608)
        {
            return null;
        }

        WebImage img = new WebImage(imageToUpload.InputStream);
        if (img.Width < 1000)
        {
            return null;
        }    

        try
        {
            //Do Something
        }
        catch (Exception ex)
        {
            //Log something
        }
        return imageFullPath;
    }
}

我尝试过传递ViewBag和TempData,但似乎都不是有效的代码?如何编写错误消息字符串并将其传递回视图?

谢谢,加文

添加控制器方法

    [HttpPost]
    public async Task<ActionResult> Upload([Bind(Include = "ID,Caption")] HttpPostedFileBase photo, PropertyImage image, int propertyId)
    {
        var imageUrl = await imageService.UploadPropertyImageAsync(photo);
        var imageGuid = Guid.NewGuid();
        image.Original_URL = imageUrl.ToString();
        image.PropertyID = propertyId;
        image.DateCreated = DateTime.Now;
        image.ID = imageGuid;
        image.Status = true;
        db.PropertyImage.Add(image);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

2 个答案:

答案 0 :(得分:1)

如果是MVC应用程序,您的操作应返回Task<ActionResult>;如果是Web API,则应返回Task<IHttpActionResult>

在您的代码中,如果未满足条件,则使用BadRequest()Controller类中存在的返回ApiController帮助程序方法返回错误请求。 您还可以在请求正文中包含其他信息。

所以,你的代码应该是这样的:

[HttpPost]
    public async Task<ActionResult> Upload([Bind(Include = "ID,Caption")] HttpPostedFileBase photo, PropertyImage image, int propertyId)
    {
        var imageUrl = await imageService.UploadPropertyImageAsync(photo);
        if(imageUrl == null)  
           return BadRequest();
        else
        {
         var imageGuid = Guid.NewGuid();
         image.Original_URL = imageUrl.ToString();
         image.PropertyID = propertyId;
         image.DateCreated = DateTime.Now;
         image.ID = imageGuid;
         image.Status = true;
         db.PropertyImage.Add(image);
         await db.SaveChangesAsync();
         return RedirectToAction("Index");
        }
    }


   public async Task<IHttpActionResult> UploadPropertyImageAsync(HttpPostedFileBase imageToUpload)
    {
        string imageFullPath = null;
        if (imageToUpload == null || imageToUpload.ContentLength == 0 || imageToUpload.ContentLength >= 8388608)
        {
            return null;
        }

        WebImage img = new WebImage(imageToUpload.InputStream);
        if (img.Width < 1000)
        {
            return null;
        }    

        try
        {
            //Do Something
        }
        catch (Exception ex)
        {
            //Log something
        }
        return  imageFullPath;
    }
}

答案 1 :(得分:0)

你的任务是返回一个字符串!

所以你可以做类似

的事情
if (imageToUpload == null || imageToUpload.ContentLength == 0 || imageToUpload.ContentLength >= 8388608)
{
            return "MY_ERROR_STRING";
}

不是最好的方式

我的建议是返回一个简单的对象

public class ResultObj(){
  public bool Success {get;set;};
  public string Result {get;set;};
}

然后测试成功,如果为true则结果为路径,如果为false,则为您的错误消息。所以你的代码看起来像

 public async Task<ResultObj> UploadPropertyImageAsync(HttpPostedFileBase imageToUpload)
    {
        ResultObj result = null;
        if (imageToUpload == null || imageToUpload.ContentLength == 0 || imageToUpload.ContentLength >= 8388608)
        {
            result.Success= False;
            result.Result = "We have error" ;
            return result;
        }

        WebImage img = new WebImage(imageToUpload.InputStream);
        if (img.Width < 1000)
        {
            result.Success= False;
            result.Result = "We have a different error" ;
            return result;
        }    

        try
        {
            //Do Something
        }
        catch (Exception ex)
        {
            //Log something
        }
        result.Success= true;
        result.Result = imageFullPath;
        return result;
    }