如何在asp.net mvc中路由“/”(默认url)

时间:2013-07-02 05:59:18

标签: c# asp.net-mvc routing

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

    }

但此测试失败

1 个答案:

答案 0 :(得分: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");

    }