Yasin
Yasin

Reputation: 1242

How to get URL of Default Action in ASP.NET MVC

I want to get URL of the default controller's default action method link, such that if I change default controller and action in route config then should be updated in the view too. e.g.

  Url.Action(defaultAction,DefaultController);
//output should be like
Url.Action("Index","Home")

Upvotes: 3

Views: 1233

Answers (1)

haim770
haim770

Reputation: 49123

Assuming the default route is named Default (in your RoutesConfig):

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

You can use Url.RouteUrl to get the url:

Url.RouteUrl("Default")

See MSDN

Upvotes: 4

Related Questions