2016-04-10 26 views
1

Aşağıdaki kodda gördüğünüz gibi ActionResult sınıfından miras alınan bir TestResult sınıfım var. ben şimdiAsp.net Core projesinde ITempDataDictionay sınıfını kullan

public static class TestExtensions 
{ 
    const string TestObject1 = "_Test1"; 
    const string TestObject2 = "_Test2"; 

    public static void AddTestObject(this ITempDataDictionary tempData, TestClass testClass) 
    { 
     if (!tempData.ContainsKey(TestObject1)) 
     { 
      tempData[TestObject1] = new List<TestClass>(); 
     } 
     ((List<TestClass>)tempData[TestObject1]).Add(testClass); 
    } 

    public static void AddTestString(this ITempDataDictionary tempData,string testString) 
    { 
     tempData[TestObject2] = testString; 
    } 

    public static ActionResult WithTestMessages(this ActionResult result) 
    { 
     return new TestResult(result); 
    } 
} 

:

public class TestResult : ActionResult 
{ 
    public ActionResult InnerResult { get; set; } 

    public TestResult(ActionResult innerResult) 
    { 
     InnerResult = innerResult; 
    } 

    public override void ExecuteResult(ActionContext context) 
    { 
     var tempDataService = context.HttpContext.RequestServices.GetRequiredService<ITempDataDictionary>(); 
     tempDataService.AddTestObject(new TestClass("TestValue1", "TestValue1")); 
     tempDataService.AddTestString("TestString1"); 
     InnerResult.ExecuteResult(context); 
    } 
} 

i bir diğeri rastgele dizedir TestClass iki nesneleri, ben tempDataService, AddTestObject ve AddTestString yöntemlerinin uygulanmasına bunları eklemek için aşağıdaki kodu olan aşağıda gördüğünüz gibi iki eylemlerle bir TestController vardır:

public class TestController : Controller 
{ 
    private readonly ITempDataDictionary _tempDataDictionary; 

    public TestController(ITempDataDictionary tempDataDictionary) 
    { 
     _tempDataDictionary = tempDataDictionary; 
    } 

    public IActionResult TestAction() 
    { 
     return RedirectToAction(nameof(TestAction2)).WithTestMessages(); 
    } 

    public IActionResult TestAction2() 
    { 
     return Content("TestAction2"); 
    } 
} 

burada garip bir şey olduğunu ben bir denetleyici içinde ITempDataDictionary enjekte ve geçmesi halinde QuickWatch penceresi ve _tempDataDictionary değerini görmek için TestObject1 için bir şey yok ve ben sadece TestObject2'yi görüyorum, bu noktada testObject1'de sınıf nesnesini görmeyi bekliyorum ve TestObject2'de dize değerini görmeyi bekliyorum.

enter image description here

cevap

1

AFAIK, mevcut uygulama ITempDataDictionary yalnızca temel değerleri kabul eder. Böylece serileştirerek ve ardından koleksiyonunuzu List<TestClass>() json'a serpiştirerek geçici bir çözüm yapabilirsiniz.

+0

Teşekkür ederim, evet bunu buldum ve yaptığınız gibi jsonConverter uygulamasını yapmak için kullandığımı söylediniz. – elhampour

İlgili konular