2012-05-30 3 views
0

바보 같은 질문 인 경우 사과드립니다.하지만 asp.net mvc3의 모델에서 오류가 발생하면 오류 페이지로 리디렉션하는 가장 좋은 방법은 궁금합니다. . 간단히 말해, "OnActionExecuting"함수에서 사용자가 Internet Explorer를 사용하고 있는지 확인하기 위해 모든 컨트롤러가 상속하는 applicationController가 있습니다.ASP.NET mvc3에서 모델에서 리디렉션

그렇다면 오류 모델 (데이터베이스에 오류를 기록하고 싶기 때문에)에서 함수를 호출합니다. 그러면 크롬을 다운로드하라는 오류 페이지로 사용자를 리디렉션해야합니다.

와 ApplicationController

public class ApplicationController : Controller 
{ 
    public string BASE_URL { get; set; } 

    public ApplicationController() {} 

    protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 

     BASE_URL = Url.Content("~/"); 

     ErrorModel err = new ErrorModel(); 

     String userAgent; 
     userAgent = Request.UserAgent; 
     if (userAgent.IndexOf("MSIE") > -1) { 
      err.cause("browser", "redirect", "some other info"); 
     }   
    } 
} 

ErrorModel 나는이 특정 예에서 알 수

public void cause(string _errCode, string strAction, string _otherInfo = "") { 

     this.error_code = _errCode; 
     this.error_otherInfo = _otherInfo; 

     try { 
      this.error_message = dctErrors[_errCode.ToLower()]; 
     } catch (KeyNotFoundException) { 
      this.error_message = "Message not found."; 
     } 

     StackTrace sT = new StackTrace(true); 
     String[] filePathParts = sT.GetFrame(1).GetFileName().Split('\\'); 

     this.error_filename = filePathParts.Last(); 
     this.error_lineNo = sT.GetFrame(1).GetFileLineNumber(); 

     this.error_function = sT.GetFrame(1).GetMethod().ReflectedType.FullName; 

     this.error_date = DateTime.Now; 
     this.error_uid = 0; //temporary 

     if (strAction == "redirect") { 
     //this is what I would like to do - but the "response" object does not 
     //exist in the context of the model 
      Response.Redirect("Error/" + _errCode);     
     } else if (strAction == "inpage") { 

     } else { 
      //do nothing 
     } 
    } 

는 오류가 실제로 모델에서 발생하지 않는, 그래서 난 그냥 컨트롤러에서 리디렉션 할 수 있습니다. 하지만 가능성이 발생할 수있는 많은 다른 오류가 필요할 것이므로 가능한 한 로그하고 리디렉션 할 함수를 호출 할 수 있어야합니다.

전적으로 잘못된 방향으로 가고있을 수도 있습니다.이 경우 나는 물론 변화를 열어두고 있습니다. 도와 주셔서 감사합니다!

+2

비즈니스 로직이 모델에 있으면 안됩니다. 그것을 컨트롤러로 옮기십시오. – Jesse

+4

비즈니스 로직이 컨트롤러에 없어야합니다! – c0deNinja

답변

1

HttpContext에서 응답에 액세스 할 수 있습니다.

HttpContext.Current.Response 
1

개인적으로 MVC 프레임 워크에서 제공하는 exception filter을 사용하여 오류 로그에 기록합니다.

<customErrors mode="RemoteOnly" defaultRedirect="~/Error/"> 

은 물론,이 모델에 가능한 단점이있다하지만 그것은 내가 지금까지 만난 예외의 대부분을 커버 : 나는 또한 웹 설정을 통해 오류보기로 리디렉션 기본값을 사용합니다.

0

ITS 훨씬 SIMPLE,

당신은

filterContext.Result = View("ErrorView", errorModel); 

을 filterContext.Result 또는

filterContext.Result = Redirect(url); 

또는

filterContext.Result = RedirectToAction("actionname"); 
를 리디렉션 결과 (ActionResult)를 지정해야합니다
관련 문제