Alisson Rubim
Alisson Rubim

Reputation: 91

MVC alternative route for custom route

i need some help. I'm working with MVC 5, in VS 2015, and i want to configure some routes in my project.

First, i have a "Default" route, that is it:

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

This route is used for the common actions, like: localhost/auth/login or **localhost/sell/`

But, I have an Area, that the name is Company. And, I want to get the name company in the url, like this: localhost/companyname/{controler}/...

So, I did something like this (in the CompanyAreaRegistration.cs) :

context.MapRoute(
    "Company_default",
    "{company}/{controller}/{action}/{id}",
    new { controller = "home", action = "Index", id = UrlParameter.Optional }
);

But, the Default route just stop working (thrown the 404 error). And, ALL the name that I put after localhost, it's calling the Company Area.

Can someone help me?

Can i do something like: try the Company route, if fail, try the Default?

Upvotes: 0

Views: 659

Answers (1)

tratatun
tratatun

Reputation: 133

Remove curly braces from company name in the template of route:

context.MapRoute("company_default",
                "company/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

Also your controller class needed the following RouteAreaAttribute:

[RouteArea("company", AreaPrefix = "company")]
public class MyTestController : Controller
{ 
    ...
}

Upvotes: 1

Related Questions