0
votes

I have an ASP.NET MVC application.

I have the routingUrl as http://myapp/Home/Products/Productname/DocTypename/CountryName Now i am trying to make the url as http://myapp/Products/Productname/DocTypename/CountryName

As I have only one controller, I want to get rid of Home directory from the Url.

Here is my code in Global.asax:

public static void RegisterRoutes(RouteCollection routes) 
    { 
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

        routes.MapRoute( 
            "Default", // Route name 
            "{controller}/{action}/{ProductName}/{DocTypename}/{CountryName}", // URL with parameters 
            new { controller = "Home", action = "Products", ProductName = UrlParameter.Optional, DocTypename = UrlParameter.Optional, CountryName = UrlParameter.Optional } // Parameter defaults 
        ); 
}

Here ProductName/DocTypename/CountryName are dynamic.

I am getting the below error when i don't specify controller in the above code:

The RouteData must contain an item named 'controller' with a non-empty string value.

Appreciate your time.

Thanks

1

1 Answers

1
votes

Try this route, but make sure its before the default route..

 routes.MapRoute( 
            "Default", // Route name 
            "{action}/{ProductName}/{DocTypename}/{CountryName}", // URL with parameters 
            new { controller = "Home", action = "Products", ProductName = UrlParameter.Optional, DocTypename = UrlParameter.Optional, CountryName = UrlParameter.Optional } // Parameter defaults 
        );