2010-01-23 8 views

답변

3

대부분의 규칙은 프레임 워크가 작동하는 방식을 알면 유연성이 있습니다. 의 가장 큰 규칙 두 가지를 해결하자

  1. 은 "{컨트롤러}/{동작} /"컨트롤러에서 첫 번째 뷰의 경로에서 프레임 워크 검색을

  2. 방법을 컨트롤러의 인스턴스를위한 마법의 키워드 디렉토리로 이동 한 다음 공유 디렉토리에 저장하십시오.

사용자가 만드는 모든 경로는 기본적으로 MvcRouteHandler 개체의 인스턴스와 연결됩니다. 경로가 일치하면 들어오는 요청을 처리하기 위해 해당 처리기가 호출됩니다.

protected internal virtual void ProcessRequest(HttpContextBase httpContext) 
{ 
    this.AddVersionHeader(httpContext); 
    string requiredString = this.RequestContext.RouteData.GetRequiredString("controller"); 
    IControllerFactory controllerFactory = this.ControllerBuilder.GetControllerFactory(); 
    IController controller = controllerFactory.CreateController(this.RequestContext, requiredString); 
    if (controller == null) 
    { 
     throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[] { controllerFactory.GetType(), requiredString })); 
    } 
    try 
    { 
     controller.Execute(this.RequestContext); 
    } 
    finally 
    { 
     controllerFactory.ReleaseController(controller); 
    } 
} 

공지 하드 코딩 된 문자열 "컨트롤러"다음은 MvcHandler의이 ProcessRequest의 모습입니다. 자, 자신 만의 컨트롤러 찾기 로직을 ​​코딩하고자한다면 원하는 모든 경로에 대해이 핸들러를 대체 할 수 있습니다. 간단히 something like this (shameless blog plug)를 수행 경로가 일치 할 때

routes.Add("ImagesRoute", 
       new Route("graphics/{filename}", new ImageRouteHandler())); 

지금, 그것은 자신의 논리를 호출하고 당신은 당신이 무엇을하시기 바랍니다 할 수 있습니다. 덧붙여 말하면, "컨트롤러"접미사가있는 XXXXController 클래스를 찾는 데 사용되는 반사는 위의 핸들러에서 호출 된 DefaultControllerFactory 객체의 일부이며이 팩토리는 대체 가능합니다.

컨트롤러 선택은 재정의 할 수있는 규칙 중 하나입니다. 어떤 컨트롤러 메소드에서 "return View()"을 수행 할 때 조회수는 언제 표시됩니까? 그럼 여기 WebFormViewEngine의 생성자, 프레임 워크의 기본보기 엔진의 :

public WebFormViewEngine() 
{ 
    base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", "~/Views/Shared/{0}.master" }; 
    base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" }; 
    base.PartialViewLocationFormats = base.ViewLocationFormats; 
} 
당신이 컨트롤러 디렉토리에보고의 규칙을 좋아하지 않는 경우에 따라서

하고 공유 - 당신은 쉽게 WebFormViewEngine을 확장 할 수 (또는 use an entirely different view engine)과 Global.asax에 그것을 풍덩 다음 MVC 프레임 워크에 대한 놀라운 일들

ViewEngines.Engines.Add(new MyViewEngine()); 

하나를 정말 얼마나 유연하다. 거의 모든 부분을 자신의 논리로 바꿀 수 있으며, 모든 코드는 그들이 한 일을 볼 수 있습니다.