تأكيد إعادة توجيه الإجراء إلى الإجراء/المسار الصحيح؟

StackOverflow https://stackoverflow.com/questions/32364

  •  09-06-2019
  •  | 
  •  

سؤال

كيف أقوم بإجراء ما للتأكد من أنه يعيد التوجيه إلى الإجراء أو المسار الصحيح؟

هل كانت مفيدة؟

المحلول

public ActionResult Foo()
{
   return RedirectToAction("Products", "Index");
}

[Test]
public void foo_redirects_to_products_index()
{
   var controller = new BarController();
   var result = controller.Foo() as RedirectToRouteResult;

   if(result == null)
      Assert.Fail("should have redirected");

   Assert.That(result.RouteData.Values["Controller"], Is.EqualTo("Products"));
   Assert.That(result.RouteData.Values["Action"], Is.EqualTo("Index"));

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top