user1570634
user1570634

Reputation: 81

MVC manipulate URL (routing), is it possible?

I have a website that use this pattern.

http://www.domain.com/product/...

My question is now, i need to create a subsite that going to be with this URL pattern, i have tried to change the routing without success.

http://www.domain.com/companyname/product/...

How can i inject the companyname in the URL without breaking my current routing?

Thanks Niden

Upvotes: 0

Views: 234

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239440

Three ways:

  1. If it's relatively static, you can follow Andy's advice in the comments and publish the site in a virtual directory, companyname. Assuming you've properly used the UrlHelper extensions to generate URLs, instead of just hard-coding paths, then everything will just work.

  2. You can create a "companyname" area. The default routing for an area is /area/controller/action. So that would get you the URL structure you want. However, areas are somewhat segregated, so you would need to copy controllers and views to the area's directory. Although, you could subclass controllers from the main app in the area to reuse code.

  3. Just change the default route/add a new route:

    routes.MapRoute(
        "CompanyDefault",  
        "{company}/{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );
    
    // default route here
    

Upvotes: 1

Related Questions