gpt4 book ai didi

c# - 为什么 RedirectToAction 返回 null?

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:10 25 4
gpt4 key购买 nike

我有以下类(class),用户将在其中访问 Controller AT/1/2,这意味着项目 1,步骤 2。如果用户进入项目而不是步骤(第二种方法/route) 它应该使用步骤 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;
}
}

最佳答案

引用 Attribute Routing in ASP.NET MVC 5 ,您的 Controller 需要一些小的更新

[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;
}
}

这里还有一些针对您的 Controller 的单元测试,在测试时全部通过(绿色)。

[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");
}
}

关于c# - 为什么 RedirectToAction 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com