1

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

如何在 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");

    }

但是这个测试失败了

4

1 回答 1

4

嘿谢谢大家的帮助

问题是我们不能在 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");

    }
于 2013-07-02T06:05:31.707 回答