1

Depending on url, I want to route different controllers. So depending on different URLs directed to same DNS server, I want to give my website a different look and feel.

To test this locally I tried:

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
        "localhost",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional }    
        );
    }
}

Wat I wanted was the Contact page to appear, not the default Home Page as the URL was :/localhost:portnr./.

How can I get the first bit of an URL (domain) to decide which controller to route?

1 Answer 1

1

Suppose you have the Contact controller and Index Action, and you want to load that on page load, you can specify as

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Contact", action = "Index", id = UrlParameter.Optional }
);
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.