tags:

views:

82

answers:

4

In the conventional website a url displayed as:

http://www.mySite.com/Topics

would typically mean a page sits in a subfolder below root named 'Topics' and have a page named default.htm (or similar).

I'm trying to get my head in gear with the MVC way of doing things and understand just enough of routing to know i should be thinking of URLs differently.

So if i have a db-driven page that i'd typically script in a physical page located at /Topics/index.aspx - how does this look in an MVC app?

mny thx --steve...

A: 

You would have an default view that is associated with an action on the Topics controller.

For example, a list page (list.aspx) with the other views that is tied to the list action of the Topics controller.

That is assuming the default routing engine rules, which you can change.

Read more here: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

Tim Hoolihan
A: 

IMHO this is what you need for your routes.

 routes.MapRoute(
     "Default",                                              // Route name
     "{controller}/{action}/{id}",                           // URL with parameters
     new { controller = "Topics", action = "Index", id = "" }  // Parameter defaults
        );

You would need a TopicsController that you build the View (topics) on.

I don't think that's a good idea to create 'generic' route with specific controller/action as default values unless these are not Home/Index.
Arnis L.
Arnis, that depends on the requirements of your application. It might not make sense to have Home/Index as the default action - instead, Topics/Index might make perfect sense. The important thing to notice is that the *default route* should always lead to somewhere where you want the users to end up *by default*. If that method is on the Topics controller, I see no problem.
Tomas Lycken
A: 

It looks just like you want it to be.

Routing enables URL to be quite virtual. In asp.net mvc it will end at specified controller action method which will decide what to do further (i.e. - it can return specified view wherever it's physical location is, it can return plain text, it can return something serialized in JSON/XML).

Here are some external links:
URL routing introduction by ScottGu
ASP.NET MVC tutorials by Stephan Walther

Arnis L.
+1  A: 

It sounds like you are used to breaking down your website in terms of resources(topics, users etc) to structure your site. This is good, because now you can more or less think in terms of controllers rather than folders.

Let's say you have a structure like this in WebForms ASP.NET.

 -Topics
   -index.aspx
   -newtopic.aspx
   -topicdetails.aspx
 -Users
   -index.aspx
   -newuser.aspx
   -userdetails.aspx

The structure in an MVC app will be pretty much the same from a users point of view, but instead of mapping a url to a folder, you map a url to a controller. Instead of the folder(resource) having files inside it, it has actions.

  -TopicController
    -index
    -new
    -details
  -UserController
    -index
    -new
    -details

Each one of these Actions will then decide what view (be this html, or json/xml) needs to be returned to the browser.

Actions can act differently depending on what HTTP verb they're repsonding to. For example;

public class UserController : Controller
{
    public ActionResult Create()
    {
        return View(new User());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(User user)
    {
        // code to validate /save user

        if (notValid)
            return new View(user);
        else
            return new View("UserCreatedConfirmation");
    }
}

This is sort of a boiled down version of RESTful URLs, which I recommend you take a look at. They can help simplify the design of your application.

Kirschstein