tags:

views:

487

answers:

3

In my web app. i want to setup a route like this:

/document/quality/index
/document/general/index

/document/quality/detail/[id]
/document/general/detail/[id]

As you can see i have two kind of documents: general and quality. What is the best way to set this up in my global.asax file? I tried the following, but i don't get it work:

routes.MapRoute(
    "QualityDocument",
    "Document/Quality/{action}/{id}",
    new { controller = "Document", action="Index", id= ""}
    );

routes.MapRoute(
    "GeneralDocument",
    "Document/General/{action}/{id}",
    new { controller = "Document", action = "Index", id = "" }
    );

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

EDIT

I have it working right now, but when i change the action="" in my asax file it doesn't work anymore:

Global.asax:

routes.MapRoute(
    "GeneralDocument",
    "Document/General/{action}/{id}",
    new { controller = "Document", action = "blaat", id = "" }
    );

DocumentController:

public void blaat(int? id)
{
    Response.Write("algemeen");
    // return View();
}

Now i get the Resource not found error. But when I use index instead of blaat it is working. How come?

A: 

Try this:

routes.MapRoute(
    "QualityDocument",
    "Document/Quality/index",
    new { controller = "Document", action="Index" }
);

routes.MapRoute(
    "Default",                                              // Route name
    "Document/Quality/details/{id}",                               // URL with parameters
    new { controller = "Document", action = "Details", id = "" }  // Parameter defaults
);
Migol
+1  A: 

Perhaps add in the controller attribute, but constrain it to be the document controller.

routes.MapRoute(
    "QualityDocument",
    "{controller}/Quality/{action}/{id}",
     new { controller = "Document", action="Index", id= ""},
     new { controller = "Document" }
);
routes.MapRoute(
    "GeneralDocument",
    "{controller}/General/{action}/{id}",
    new { controller = "Document", action = "Index", id = "" },
    new { controller = "Document" }   );
tvanfosson
What do you mean by add in the controller attribute? How does this work?
Martijn
I just mean put {controller} in your pattern. I was wondering if it wasn't able to pull the controller value since it didn't appear in your pattern.
tvanfosson
After thinking about it a little, don't you need to pull out the document type property to know how to differentiate between the two? This might require a custom route handler to do properly.
tvanfosson
To be honest, i don't know. I'm new to MVC. I don't know much about it at all..
Martijn
If have two types of documents and /Document/MMM/Index both route to the Index action on the Document controller, how will you know which MMM-type to index without having a "type" route value?
tvanfosson
what do you mean with "type" route?
Martijn
+1  A: 

you should be able to just prefix the route with "Document" and then set the defaults like:

 routes.MapRoute("DocumentView", 
                "Document/{controller}/{action}/{id}", 
                new { controller = "General", action="Index", id = ""  }
            );

remember with routing you want to be completely defining! when setting out the route. dont let those unexpected routes through :D

Check out Scott Hanselman presentation at MIX very funny and your pick up some great tips!

http://www.hanselman.com/blog/Mix09FirstHalfRollupAndSessionVideos.aspx

Mike