3

As we all know Asp.net MVC creates some defaults for us when we create a new Asp.net MVC project:

  • Default controller is HomeController
  • Default action Index

But I've noticed that although Stackoverflow did keep the HomeController it didn't keep Index as the default action.

http://www.stackoverflow.com/home - works
http://www.stackoverflow.com/home/index - fails

I don't like the name (Index) of the default action either, that's why I'm looking for other possibilities and am wondering what's Stackoverflow's default action name?

1
  • Err... I didn't know that.
    – innaM
    Oct 28, 2009 at 19:27

2 Answers 2

3

It can indeed Be index but if they remove the default route or replace it with the following code, then you have the same effect.

Since stackoverflow.com/Home we know that they use this route:

routes.MapRoute(
    "Default", "{controller}",
    new { controller="", action="Index" }
);

What this route does, is that if you have what would normally be:

stackoverflow.com/home/index
stackoverflow.com/questions/ndex
stackoverflow.com/users/index

You'd be able to get to the index action by omitting the index. Indeed, if you took out the default route, that's the way you'd get to the index action:

stackoverflow.com/home
stackoverflow.com/questions
stackoverflow.com/users

To answer your question, I gather that they also have the following route:

routes.MapRoute(
    "Default", "",
    new { controller = "Home", action = "Index" } 
);
10
  • I don't think you understood my question. I was basically asking how is default route defined on Stackoverflow. Because controller is "Home" but action IS NOT "Index". Oct 28, 2009 at 18:00
  • 1
    @Robert Kortinik: And I'm telling you you can define the action as 'Index' and still take out the /Index path and have it work. Oct 28, 2009 at 18:04
  • @George: Yes I know that. But you can see from my question that if you don't omit "Index" from the URL you get a 404. So it clearly tells me that they are using something else instead of "Index". I'm trying to find out what. Oct 28, 2009 at 18:07
  • @Robert: If you take out the routing or change how the routing is done for the Index action, you can indeed get it to throw an error or not be able to handle /index but still Have the action named 'index'. I don't think you understand how routing works. Oct 28, 2009 at 18:08
  • @George: I do understand routing very well. It seems we don't really understand each other. I'm getting confused by your answers. Are you actually saying that Stackoverflow does have /Home/Index combination? Because it seems they don't since I'm getting 404. Unless of course if it's just POST or something similar. Oct 28, 2009 at 18:36
  • You can have a Home/Index but if you remove the Default route; then you wouldn't be able to get to it (and get the error you see). If you change the default route to what I wrote, then the only way to get to the Index action is to go to stackoverflow.com/ Oct 28, 2009 at 18:38
  • @George: Yes. My fault. I missed the cleared URL that's usually set as "{controller}/{action}/{id}". You may be right, but I don't think they'd do something like that just to hide Index action... And to hide Index action onlym your call should define URL as "{controller}/", because /Home is working as expected. Oct 28, 2009 at 18:57
  • The beautiful thing about routing is that I can route any URL To anywhere I want. I'll update my answer to show you how they do it. Oct 28, 2009 at 19:03
  • Not actually the answer I was looking for, but this is one possibility that we don't know whether it's actually done on SO or not. I was hoping someone on SO team would actually answer this one. Oct 28, 2009 at 19:13
  • 1
    @Robert Koritnik the great thing about MVC is that you can pretty much guess how the site is laid out from the routes that work and from the nature of MVC. I'd be very surprised if they didn't do it this way -- though I imagine they probably have regular expressions attached to the route as well. Oct 28, 2009 at 19:14
0

I don't know but I hope they've filtered out the exceptions caused by invalid controller/action parameters from their ELMAH email. I imagine this question will create a flood of invalid requests as people try to guess the index action.

1
  • So better for them to share the info asap... ;) Oct 28, 2009 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .