2016-09-13 16 views
0

我在Asp.Net MVC 5中有一個標準的編輯動作,並且我想避免在沒有像~/food/edit這樣的標識符的請求時發出未處理的異常,所以我這樣做了。如何保護編輯操作路徑中的Asp.Net MVC 5不會引發未處理的異常?

public ActionResult Edit(int id = 0) 
    { 
     if (id == 0) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 

     string result = _foodAppService.GetById(id); 
     FoodVm food = string.IsNullOrEmpty(result) 
      ? null 
      : JsonConvert.DeserializeObject<FoodVm>(result); 

     if (food == null) 
     { 
      return RedirectToAction("Index"); 
     } 

     return View(food); 
    } 

我的問題是:以這種方式處理它或者有更合適的策略是一種好的做法嗎?

我是新來的這個問問題的事情,如果我應該以另一種方式問,請讓我知道,謝謝你的時間。

+1

那麼,這裏的問題是任何答案都將不可避免地被置之度外,這超出了Stack Overflow的範圍。相反,您可能想將問題提交給Code Review。 –

+1

你可以創建一個動作過濾器,然後將其應用到你想要的方法。你也不需要初始化int爲0,int會自動初始化爲0.你怎麼能首先得到這種情況?你不是在創建編輯鏈接嗎?你是否擔心人們只是在輸入胡言亂語? – Fran

+0

@ChrisPratt,我不知道有這樣的事情,我只是張貼在那裏,謝謝,因爲我說:我是新來的這個問題。 – PedroSouki

回答

1

在零的情況下可以有效的更好的事情可做

public ActionResult Edit(int? id) 
{ 
    if (!id.HasValue) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
} 
0

或有可能在MVC來處理異常更全面的方式。您可以使用覆蓋方法OnException,這使您可以調試一個方法中的控制器中的所有異常並處理它們。 只需添加基類所有的控制器是這樣的:

public class BaseController : Controller 
{ 
    protected override void OnException(ExceptionContext filterContext) 
    { 
     string redirectUrl; 
     var exception = filterContext.Exception; 

     if (exception is EntityException) 
     { 
      redirectUrl = "/Content/error.html"; 
     } 
     else 
     { 
      redirectUrl = "/Info/Index"; 
     } 

     //do whatever you wont 

     Response.Redirect(redirectUrl); 
    } 

還可以使用輸入參數驗證保羅Swetz Saied如何。這種方法更通用,可以截取所有異常,並且不會向用戶顯示錯誤。

-1

首先它最好使用Try-Catch。

public ActionResult Edit(int id) 
{ 
    try 
    { 
      if (id != 0 || id!=null) 
      { 
      string result = _foodAppService.GetById(id); 
      FoodVm food = string.IsNullOrEmpty(result) ? null:JsonConvert.DeserializeObject<FoodVm>(result); 

      if (food == null) 
      { 
       return RedirectToAction("Index"); 
      } 
      else 
      {  
      return View(food); 
      } 
      } 
     else 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
    } 
    catch (exception ex) 
    { 
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
} 
+2

請**,不要**如果你真的不需要它,請使用try catch。它真的很慢,並且你的代碼真的很髒。 閱讀次數: https://msdn.microsoft.com/en-us/library/ms173162.aspx https://msdn.microsoft.com/en-us/library/ms173160(VS.80).aspx –

+0

再加上,你的try/catch塊覆蓋了永遠不會拋出異常的代碼。把你寫的所有內容放到try/catch塊中並不是一個好習慣。處理例外情況,例如特殊情況(這是他們應該做的)。 – jwiscarson

+0

如果你要從數據庫中獲取數據,試着抓住就是救星。你永遠不知道DB中會出現什麼問題。它也有助於有一個記錄器來跟蹤錯誤日誌。 –

0

按照@Fran的建議。我內置叫MissingParam

public class MissingParamAttribute : ActionFilterAttribute 
{ 
    public string ParamName { get; set; } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (filterContext.ActionParameters.ContainsKey(ParamName)) 
     { 
     if (filterContext.ActionParameters[ParamName] == null) 
     { 
      filterContext.ActionParameters[ParamName] = 0; 
     } 
     } 

    base.OnActionExecuting(filterContext); 
    } 
} 

一個動作過濾器屬性的動作我做:

[MissingParam(ParamName="id")] 
public ActionResult Edit(int id) 

這樣,我不需要用方法的參數混亂,任何驗證之前發生。該實現遵循打開/關閉原則。我擴展了它的功能,但是我沒有像問題中的代碼那樣改變。

相關問題