ASP.NET MVC 4: keeping Index as defaul view
By default MVC 4 application have the following routes in
ProjectFolder\App_Start\RoutConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
In browser, any call to http://host:port/controller/ would take you to
http://host:port/controller/Index.
Now, if you want to change the start page, lets say
SomeController\MyAction, you can change it like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "SomeController", action = "MyAction", id
= UrlParameter.Optional }
);
But then http://host:port/controller/ won't display
http://host:port/controller/Index but the 404 error page. You would need
to explicitly append /Index in URL.
If you change the startup page from project properties and keep the routes
to Home\Index, then everything would work fine except the URL root
http://host:port/ won't display the startup page. When you run the
application it will append the startup page name in the URL. But if you
have relative hyperlink like <a href="/" title="home">Home</a> somewhere
in your application, it would not take you to the desired homepage
Is there a way to configure the startup page "SomeController\MyAction"
such that; root URL always point to it and the Index be the default view
of every controller?
Something equivalent to Ruby on Rails' route:
root :to => "some_controller#my_action"
which keeps the default view binding to index action.
No comments:
Post a Comment