c# - Why does empty controllerName property work when the default action is empty? -
i reading on urls , routes chapter in pro asp.net mvc 3 , tried see happens when object defaults contain empty values. route have @ moment.
routes.maproute("myroute", "{controller}/{action}", new { controller="home", action="index" });
following observations made possible combinations of values of object defaults. (in each case, urls in bold not accessible.)
case 1
new { controller="home", action="index" });
case 2
new { controller="home", action="" });
- http://mywebapp.net/
error: routedata must contain item named 'action' non-empty string value. - http://mywebapp.net/home/
error: routedata must contain item named 'action' non-empty string value. - http://mywebapp.net/home/index/
the above error 2nd url because routing system couldn't find default action name.
case 3
new { controller="", action="" });
- http://mywebapp.net/
error: value cannot null or empty. parameter name: controllername - http://mywebapp.net/home/
error: routedata must contain item named 'action' non-empty string value. - http://mywebapp.net/home/index/
the above error 1st , 2nd urls because routing system couldn't find default controller , action names respectively.
case 4
new { controller="", action="index" });
- http://mywebapp.net/
error: value cannot null or empty. parameter name: controllername - http://mywebapp.net/home/ (how , why accessible?)
- http://mywebapp.net/home/index/
when both controller
, action
properties empty in case 3, received errors expected. in case 4, how 2nd url http://mywebapp.net/home/ accessible when have empty value controller
property?
is simple 2nd url being accessible because homecontroller defined , routing system found convention or there explanation behavior? can modified let 2nd url inaccessible or violate convention-over-configuration principle?
home
, index
not hardcoded conventions, unless specify them defaults runtime won't attempt find them.
case 2.1 not work because url did not contain value action
parameter, , there's no action
default.
case 4.1 not work because url did not contain value controller
parameter, , there's no controller
default.
i recommend this post if want more details how routing works.
Comments
Post a Comment