为什么RedirectToAction返回null?

时间:2016-04-23 15:42:23

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing

我有以下类,用户将访问控制器AT/1/2,这意味着项目1,步骤2.如果用户输入项目而不是步骤(第二种方法/路线),则应使用步骤99如果用户没有输入任何东西,它应该使用项目1,步骤99.我可以通过手动调用重载方法轻松解决这个问题,但我认为我应该能够用 RedirectToAction 。但是在第二和第三种方法中, v 在我的单元测试中为空。

我的RegisterRoutes看起来像:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes(); //MVC 5 Attribute Routes

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

我不确定我在这里失踪了什么。

public class ATController : Controller
{
    [AllowAnonymous]
    [Route("{project}/{step}")]
    public ActionResult Index(int project, int step)
    {
        var m = new ATViewModel();
        m.Project = project;
        m.Step = step;
        return View(m);
    }

    [AllowAnonymous]
    [Route("{project}")]
    public ActionResult Index(int project)
    {
        var v = RedirectToAction("Index", new { project, step = 99 });
        return v;
    }

    [AllowAnonymous]
    public ActionResult Index()
    {
        var v = RedirectToAction("Index", new { project = 1, step = 99 });
        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

引用Attribute Routing in ASP.NET MVC 5,您的控制器需要进行一些小更新

[RoutePrefix("at")]
public class ATController : Controller
{
    //GET /at/1/2
    [AllowAnonymous]
    [Route("{project}/{step}")]
    public ActionResult Index(int project, int step)
    {
        var m = new ATViewModel();
        m.Project = project;
        m.Step = step;
        return View(m);
    }

    //GET /at/1
    [AllowAnonymous]
    [Route("{project}")]
    public ActionResult Index(int project)
    {
        var v = RedirectToAction("Index", new { project = project, step = 99 });
        return v;
    }

    //GET /at
    [AllowAnonymous]
    [Route("")]
    public ActionResult Index()
    {
        var v = RedirectToAction("Index", new { project = 1, step = 99 });
        return v;
    }
}

以下是您的控制器的一些单元测试,测试时全部通过(绿色)。

[TestClass]
public class MvcControllerAttributeRouteTests : ControllerUnitTests {
    [TestMethod]
    public void Index_Should_Return_ViewResult_With_Model() {
        //Arrange
        int project = 1;
        int step = 2;
        var controller = new ATController();
        //Act
        var actionResult = controller.Index(project, step);
        var viewResult = actionResult as ViewResult;
        //Assert
        Assert.IsNotNull(viewResult);
        Assert.IsNotNull(viewResult.Model);
        Assert.IsInstanceOfType(viewResult.Model, typeof(ATViewModel));
        var viewModel = viewResult.Model as ATViewModel;
        Assert.AreEqual(project, viewModel.Project);
        Assert.AreEqual(step, viewModel.Step);
    }

    [TestMethod]
    public void Project_Supplied_No_Step_Should_Redirect_To_Index() {
        //Arrange
        int project = 1;
        var controller = new ATController();
        //Act
        var actionResult = controller.Index(project);
        var result = actionResult as RedirectToRouteResult;
        //Assert
        Assert.IsNotNull(result);
        Assert.AreEqual("Index", result.RouteValues["action"], "the redirection was to at.index action");
        Assert.AreEqual(project, result.RouteValues["project"]);
    }

    [TestMethod]
    public void No_Porject_No_Step_Should_Redirect_To_Index() {
        //Arrange
        var controller = new ATController();
        //Act
        var actionResult = controller.Index();
        var result = actionResult as RedirectToRouteResult;
        //Assert
        Assert.IsNotNull(result);
        Assert.AreEqual("Index", result.RouteValues["action"], "the redirection was to at.index action");
        CollectionAssert.Contains(result.RouteValues.Keys, "project");
        CollectionAssert.Contains(result.RouteValues.Keys, "step");
    }
}