tags:

views:

167

answers:

4

This is a basic question about the routing machinery. From a new MVC project, I have this in the HomeController:

public ActionResult MyPage()
{
  return View();
}

In the Views\Home folder, I have MyPage.aspx. The routing is still the default of {controller}/{action}/{id}. When I type in http://localhost:1790/Home/MyPage, it is correclty routes to MyPage.aspx. Since I haven't said anything about which view to render, how does ASP.NET MVC know to correctly route to MyPage.aspx? Looks as though the ActionResult name can also be used as the View/aspx page name...unless there is something I misunderstand in how the routing works. I can see how I end up in the Home folder, since the controller name corresponds to the View sub folder name. But does the Action name also correspond to the aspx name?

Would that work if the page was a PHP?

+1  A: 

MVC implicitly assumes that if you return just View(), that you want View("MyPage") (i.e. the action name). No sense in repeating yourself unnecessarily.

It won't find a PHP file by default, but I'm sure you could override that behavior if you really wanted to. I can't imagine a sane scenario where you would be mixing PHP and ASP.NET MVC, but who knows :)

mgroves
+1  A: 

Action name is the same as the view / partial view name.

asp.net mvc doesn't work with php as far as I'm aware.

DeletedAccount
+3  A: 

ASP.NET MVC subscribes to what is known as the Convention over Configuration paradigm whereas if you follow their conventions, basic things such as routing concerns will happen for you. But they also allow you to configure them if desired.

Jon Erickson
+1  A: 

As has already been stated, ASP.NET MVC uses convention over configuration. Out of the box, your folder structure is something like this (only showing relevant portions and doing it from memory so...)

Site Root

+ Controllers
    HomeController.cs
    AccountController.cs
+ Views
  + Home
      Index.aspx
  + Account
      Index.aspx
  + Shared

The default routing handler is something similar to the following:

  "{controller}/{action}/{id}"

There are default values for the route, but if you have a url that is a/b/c, it will look for action a on controller aController and pass it c as a parameter if said method on the controller accepts parameters.

A couple of things about that then need to be clarified. Again, convention over configuration: 1) All controller classes must end with Controller if you're using the default engine. That way, when a request comes in and the {controller} value is parsed, the engine adds Controller to it, looks in the Controller folder (and, thus, namespace), and locates the class. 2) By default -- this can be changed -- all views for a controller must reside in the Views/{controller} folder or in the Views/Shared folder. 3) Public methods on a controller are, by default, actions. You can hide this with an attribute to make themethod unavailable to the engine, but by default they are public.

So, when a request comes in the route is compared against all known routes (global.asax) and the first route that matches the request is accepted. The route is then parsed into the component parts to determine the controller, action, and parameters for the action.

Once the controller is identified, the engine instantiates an instance of that controller and executes the matching method (action) should it be found.

The action will return an ActionResult. View is an extensino method that actually returns ViewResult (if I remember that correctly). The default view for an action is a view of the same name as teh action residing in the Views/{ControllerName} folder.

Routing is a beast unto itself and I'd recommend a good bit of reading on it if you're going to play with it. Minutes to understand but a lifetime to master sorta thing.

To my knowledge, BTW, there is no engine that will use a php page as a view for a controller action.

andymeadows