gpt4 book ai didi

c# - 如何在 asp.net mvc 中路由 "/"(默认 url)

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

我正在尝试将“/”路由到某个虚拟网址,例如 www.abc.com/india,但我无法路由它。

如何在 MVC 中路由默认 Url。

我已经写好了这是route.config

routes.MapRoute(
name: "HomePage",
url: "/",
defaults: new { controller = "Home", action = "index" }
);

我的测试函数是

    [TestMethod]
public void Home_Index_test_with_no_Parameter()
{
var context = new StubHttpContextForRouting(requestUrl: "~/");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);

// Act
var routeData = routes.GetRouteData(context);

// Assert
Assert.IsNotNull(routeData, "~/ url is getting routed properly");
Assert.AreEqual("home", routeData.Values["controller"],
"~/ url is not getting routed properly");
Assert.AreEqual("index", routeData.Values["action"], "~/ url is not getting routed properly");

}

但是这个测试失败了

最佳答案

谢谢大家的帮助

问题是我们不能在 url 中使用“/”作为前缀来路由。下面是工作代码

routes.MapRoute(
name: "HomePage",
url: "",
defaults: new { controller = "Home", action = "index" }
);

对其进行单元测试

    [TestMethod]
public void Home_Index_test_with_no_Parameter()
{
var context = new StubHttpContextForRouting(requestUrl: "~/");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);

// Act
var routeData = routes.GetRouteData(context);

// Assert
Assert.IsNotNull(routeData, "~/ url is getting routed properly");
Assert.AreEqual("Home", routeData.Values["controller"],
"~/ url is not getting routed properly");
Assert.AreEqual("index", routeData.Values["action"], "~/ url is not getting routed properly");

}

关于c# - 如何在 asp.net mvc 中路由 "/"(默认 url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17418375/

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