frc
frc

Reputation: 588

Get first url segment

How would I go about changing the default controller/action and RouteConfig.cs so that I always display the first url segment on the page?

So if the url is mysite.com the page should be blank, if it is mysite.com/somepage I should get somepage printed on the page, if the page is mysite.com/services I should get services printed on the page and so on.

The default controller is Home and the default method action is Index.

I guess the method would go something like this:

public ActionResult Index(string page = "")
        {
            return Content(page);
        }

But what about RouteConfig.cs ?

Upvotes: 1

Views: 562

Answers (1)

Henrique Forlani
Henrique Forlani

Reputation: 1353

You should erase the default route and add your custom ones, like this:

// Home route
routes.MapRoute("Home", "", new { controller = "Home", action = "Index" });

The second parameter in routes.MapRoute is the url, which in this case is empty. For services you would have your route like this:

// Services route
routes.MapRoute("Services", "services", new { controller = "Some Controller", action = "Some action" });

Upvotes: 1

Related Questions