2016-12-14 7 views
0

イメージをアップロードするために私のコントローラから呼び出されるサービスがあります。これはうまくいきますが、基準が満たされていない場合、現在は「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"); 
    } 

答えて

1

ようになります。

コードでは、条件が満たされない場合、ControllerApiControllerクラスの両方に存在する返信BadRequest()ヘルパーメソッドを使用して、不正なリクエストを返します。 リクエスト本体に追加情報を含めることもできます。

だから、あなたのコードは次のようにする必要があります:

[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; 
    } 
} 
+0

しかし、このコードは、コントローラに存在しない、それがコントローラ「から」と呼ばれます。だからBadRequest()を返すことはできません。 – Gavin5511

+0

コントローラから呼び出されたのはどういう意味ですか?これはヘルパーメソッドであり、コントローラアクションか何かから呼び出されているということですか? –

+0

私はオリジナルの質問にコントローラコードを追加しました – Gavin5511

0

あなたの仕事は、文字列を返します!

ですから、

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

なく、私のアドバイスはtrueの場合は、成功のためのテストを単純なオブジェクト

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

を返すようになり、すべての

で最良の方法のような何かを行うことができます結果はfalseの場合はエラーメッセージです。ので、あなたのコードは、MVCアプリケーションまたはTask<IHttpActionResult>であれば、それはウェブAPIだった場合あなたのアクションがTask<ActionResult>を返すべき

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; 
    } 
関連する問題