GetVaryByCustomString
이 (가) 여기에 오는 방법입니다. 내 개념 증명은 다음으로 구성되었습니다.
- WebUserControl.ascx : 테스트 컨트롤. 그것에는 하나의 공공 재산
MatchDescription
가 있습니다.
- Global.asax :
GetVaryByCustomString
메서드를 재정의합니다.
- WebForm.aspx : 컨트롤을 호스트하는 간단한 폼.
는
는 컨트롤의 마크 업에 다음을 추가 WebUserControl.ascx : 컨트롤을 캐시 이것은 (초) 기간을 지정
<%@ OutputCache Duration="120" VaryByParam="none" VaryByCustom="MatchDescription" %>
및 VaryByCustom="MatchDescription"
이의 이름을 지정합니다 우리가 캐싱 할 매개 변수.
WebUserControl.ascx.cs
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public string MatchDescription { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
object description = this.Context.Application["MatchDescription"];
if (description != null)
{
this.MatchDescription = description.ToString();
}
else
{
this.MatchDescription = "Not set";
}
Label1.Text = "Match description: " + this.MatchDescription;
}
}
이것은 MatchDescription
값의 존재하지 않는지 확인합니다. 상위 페이지의 코드가 작동하는 방식 때문에 "설정되지 않음"이 표시되지 않아야합니다. 구현에서 값이 설정되지 않은 경우 유용 할 수 있습니다. Global.asax에
프로젝트에 Global.asax
파일을 추가하고 다음과 같은 방법으로 추가
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "MatchDescription")
{
object description = context.Application["MatchDescription"];
if (description != null)
{
return description.ToString();
}
}
return base.GetVaryByCustomString(context, custom);
}
이 캐시 된 컨트롤과 관련된 MatchDescription
를 확인하는 비트입니다. 발견되지 않으면 컨트롤이 정상적으로 생성됩니다. context.Application
은 부모 페이지, 사용자 정의 컨트롤 및 global.asax 파일간에 설명 값을 전달하는 방법이 필요하기 때문에 사용됩니다.
WebForm.aspx.cs
public partial class WebForm : System.Web.UI.Page
{
private static string[] _descriptions = new string[]
{
"Description 1",
"Description 2",
"Description 3",
"Description 4"
};
protected override void OnPreInit(EventArgs e)
{
//Simulate service call.
string matchDescription = _descriptions[new Random().Next(0, 4)];
//Store description.
this.Context.Application["MatchDescription"] = matchDescription;
base.OnPreInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
var control = LoadControl("WebUserControl.ascx") as PartialCachingControl;
this.Form.Controls.Add(control);
//Indicate whether the control was cached.
if (control != null)
{
if (control.CachedControl == null)
{
Label1.Text = "Control was cached";
}
else
{
Label1.Text = "Control was not cached";
}
}
}
}
이 코드에 내가/만들기 OnPreInit
방법에서 서비스 호출을 시뮬레이션하고 있습니다.이는 GetVaryByCustomString
메서드 이전의 페이지 수명주기에서 발생하므로 필요합니다.
if (control is PartialCachingControl &&
((PartialCachingControl)control).CachedControl =!= null)
{
WebUserControl1 userControl = (WebUserControl1)((PartialCachingControl)control).CachedControl;
}
참고 :
는 제어가 캐시 된 경우 Page_Load
방법에 접근, 예를 들어,이 양식의 코드가 필요합니다 것을 명심하십시오 내 대답은 다음과 같습니다. Any way to clear/flush/remove OutputCache? 안녕하세요. Pre_Init
안녕하세요. NT이 질문에 : Output Caching - GetVaryByCustomString based on value set in PageLoad()
PartialCachingControl.CachedControl
속성은 항상 null를 돌려 수있는 이유 기술 자료 문서가 설명 : http://support.microsoft.com/kb/837000
값에 대한'에서 오는 MatchDescription'는 무엇입니까? 예 : 부모 페이지가 URL에서 가져온 다음 하위 컨트롤의 속성을 설정합니까? –
둘러보기 주셔서 감사합니다! MatchDescription의 값은 부모 사용자 컨트롤에서 가져옵니다. 아니오, 쿼리 문자열이 아닌 페이지에서 오지 않습니다. –
문제는 없습니다.) 부모 컨트롤이 값을 가져 오는 위치에 대한 세부 정보를 더 제공 할 수 있습니까? 이 데이터 조각을 언제 어디서 사용할 수 있는지 쉽게 알면 해결할 수 있습니다. –