0

그래서 한 번 판매하려는 웹 응용 프로그램을 만들고 있습니다. 그것은 사용자가 관리자 패널에 자신의 웹 사이트 이름, 메타 키워드, 연락처 이메일, 전화 번호, 주소 등의 데이터를 입력 할 수 있습니다. 필자는 필자가 필터를 쓴 모든 요청에이 값을 포함시키기 위해 액션 필터를 작성했습니다. 따라서이 값은 사이트 전체의 공통 푸터에 포함되므로 매번 쿼리해야했습니다. 그러나이 값에 대한 새 정보 나 다른 정보로 데이터베이스를 업데이트하면 애플리케이션 시작시 작업 필터가 구성되기 때문에 추측 할 수있는 웹 페이지에서는 업데이트되지 않는다는 것을 알게되었습니다. 액션 필터에서 저장소 패턴을 사용하여이 값을 쿼리합니다. 아래 액션 필터 코드를 포함 시켰습니다. 액션 필터의 편리 성을 어떻게 얻을 수 있습니까?하지만 데이터베이스에서 데이터가 변경되면 동적으로 업데이트 할 수 있습니까? 감사!ASP.NET MVC 4 동적 데이터가있는 사용자 지정 동작 필터

public class ViewBagActionFilter : ActionFilterAttribute,IActionFilter 
{ 
    Repositories.SettingsRepository _repo = new Repositories.SettingsRepository(); 

    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     base.OnActionExecuted(filterContext); 
    } 

    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string siteName = _repo.GetSiteName(); 
     string siteDesc = _repo.GetSiteDescription(); 
     string siteKeywords = _repo.GetSiteKeywords(); 
     string googleAnalytics = _repo.GetGoogleAnalytics(); 
     string streetAddress = _repo.GetStreetAddress(); 
     string zipCode = _repo.GetZipCode(); 
     string city = _repo.GetCity(); 
     string state = _repo.GetState(); 
     string aboutUs = _repo.GetAboutUs(); 
     string phone = _repo.GetPhoneNumber(); 
     string contactEmail = _repo.GetContactEmail(); 

     if (!string.IsNullOrWhiteSpace(siteName) && siteName.Length > 0) 
     { 
      string[] splitSiteName = new string[siteName.Length/2]; 
      splitSiteName = siteName.Split(' '); 
      if (splitSiteName.Length > 1) 
      { 
       filterContext.Controller.ViewBag.SiteName1 = splitSiteName[0]; 
       filterContext.Controller.ViewBag.SiteName2 = splitSiteName[1]; 
      } 
      else 
      { 
       filterContext.Controller.ViewBag.SiteName1 = splitSiteName[0]; 
       filterContext.Controller.ViewBag.SiteName2 = ""; 
      } 
     } 
     //Set default values for common viewbag items that are on every page using ternary syntax 
     filterContext.Controller.ViewBag.SiteDescription = (!string.IsNullOrWhiteSpace(siteDesc) && siteDesc.Length > 0) ? siteDesc : ""; 
     filterContext.Controller.ViewBag.SiteKeywords = (!string.IsNullOrWhiteSpace(siteKeywords) && siteKeywords.Length > 0) ? siteKeywords : ""; 
     filterContext.Controller.ViewBag.GoogleAnalytics = (!string.IsNullOrWhiteSpace(googleAnalytics) && googleAnalytics.Length > 0) ? googleAnalytics : ""; 
     filterContext.Controller.ViewBag.StreetAddress = (!string.IsNullOrWhiteSpace(streetAddress) && streetAddress.Length > 0) ? streetAddress : ""; 
     filterContext.Controller.ViewBag.ZipCode = (!string.IsNullOrWhiteSpace(zipCode) && zipCode.Length > 0) ? zipCode : ""; 
     filterContext.Controller.ViewBag.City = (!string.IsNullOrWhiteSpace(city) && city.Length > 0) ? city : ""; 
     filterContext.Controller.ViewBag.State = (!string.IsNullOrWhiteSpace(state) && state.Length > 0) ? state : ""; 
     filterContext.Controller.ViewBag.AboutUs = (!string.IsNullOrWhiteSpace(aboutUs) && aboutUs.Length > 0) ? aboutUs : ""; 
     filterContext.Controller.ViewBag.PhoneNumber = (!string.IsNullOrWhiteSpace(phone) && phone.Length > 0) ? phone : ""; 
     filterContext.Controller.ViewBag.ContactEmail = (!string.IsNullOrWhiteSpace(contactEmail) && contactEmail.Length > 0) ? contactEmail : ""; 
     base.OnActionExecuting(filterContext); 
    } 
} 

답변

1

조치 필터의 작동 원리를 설명하려고합니다. - 컨트롤러 액션이 실행되기 전에이 방법이라고

  • OnActionExecuting :

    그래서 당신은 작업 필터링 확장 할 경우 4 개 기본 방법을 대체 할 수 있습니다.

  • OnActionExecuted -이 메서드는 컨트롤러 동작이 실행 된 후에 호출됩니다.
  • OnResultExecuting -이 메서드는 컨트롤러 동작 결과가 실행되기 전에 호출됩니다.
  • OnResultExecuted -이 메소드는 제어기 조치 결과가 실행 된 후에 호출됩니다.

이렇게하면 컨트롤러가 작업을 실행하기 전에 매번 메서드가 호출됩니다.

최적화에 대해 설명합니다. 당신은

string siteName = _repo.GetSiteName(); 
     string siteDesc = _repo.GetSiteDescription(); 
     string siteKeywords = _repo.GetSiteKeywords(); 
     string googleAnalytics = _repo.GetGoogleAnalytics(); 
     string streetAddress = _repo.GetStreetAddress(); 
     string zipCode = _repo.GetZipCode(); 
     string city = _repo.GetCity(); 
     string state = _repo.GetState(); 
     string aboutUs = _repo.GetAboutUs(); 
     string phone = _repo.GetPhoneNumber(); 
     string contactEmail = _repo.GetContactEmail(); 

난 당신이

다음
public class Site{ 
public string SiteName{get;set;} 
public string City{get;set;} 
//And so on just to add all properties 
} 

저장소에 아마도 지금

_repo.GetSite(); //Which will return object Site 

filterContext.Controller.ViewBag.CurrentSite = _repo.GetSite(); 

그런

그리고 또 하나의 방법을 추가 한 클래스를 만드는 제안했다 당신에게 가장 중요합니다. 왜 그것이 당신이 원하는대로 작동하지 않습니다 그리고 조금은 간단합니다. 속성 클래스는 응용 프로그램 시작에 한 번만 초기화되고 그 후에는 다시로드를 doesnot 및 구현은 설정을로드하는 내가 여기에 가정

Repositories.SettingsRepository _repo = new Repositories.SettingsRepository(); 

이후 이상한 조금이다. 따라서로드 한 후에 더 이상 페이지를 다시로드하지 않아도됩니다. 즉, 페이지를 새로로드 할 때마다 동일한 결과가 표시되지만 iis를 다시 시작하면 데이터를 새로 고칩니다.OnActionExecuting에 _repo의 가능한 해결 방법

이동 초기화 후 데이터 매번 다시로드, 또는 저장소를 다시 내가 제안하고

filterContext.Controller.ViewBag.CurrentSite = _repo.GetSite(); 

는 항상 DB에서 새로운 데이터를로드해야합니다.

희망 하시겠습니까?

관련 문제