我正在尝试将“/”路由到某个虚拟网址,例如 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");
}
