1

I am trying to route "/" to some virtual url say www.abc.com/india, but u i am not able to route it.

How can I route the default Url in MVC.

I have written this is route.config

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

and my test function is

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

    }

but this test fails

2
  • Where it fails? Any chance to debug? Or maybe check for controller name as one you specify (instead of differently cased home)? Commented Jul 2, 2013 at 6:02
  • Thanks for the help, I had given the answer you can check if you want Commented Jul 2, 2013 at 6:06

1 Answer 1

4

Hey thank every one for help

The problem is that we cant use "/" as prefix in url to route. below is the working code

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

Unit Test for it

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

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.