views:

59

answers:

1

I'm using areas everywhere and I'm wanting something like the following:

http://localhost/MyArea/MySection/MySubSection/Delete/20

Usually I access things by doing the following:

http://localhost/MyArea/MySection/MySubSection/20

But if I want to delete then I have to say

http://localhost/MyArea/MySection/DeleteEntryFromMySubSection/20

With routes, how do you do this? (the routes aren't realistic by the way, they're much more concise than this in my system)

EDIT: This is specifically related to the use of Areas, an ASP.NET MVC 2 Preview 2 feature.

A: 

It would depend on how your routes & controllers are currently structured.

Here's an example route you might want to use.

If you want to be able to call the following route to delete:

http://localhost/MyArea/MySection/MySubSection/Delete/20

And let's assume you have a controller called "MyAreaController", with an action of "Delete", and for the sake of simplicity let's assume section and subsection are just strings e.g.:

public class MyAreaController : Controller
{
    public ActionResult Delete(string section, string subsection, long id)
    {

Then you could create a route in the following way (in your Global.asax.cs, or wherever you define your routes):

var defaultParameters = new {controller = "Home", action = "Index", id = ""};            

routes.MapRoute("DeleteEntryFromMySubSection", // Route name - but you may want to change this if it's used for edit etc.
            "{controller}/{section}/{subsection}/{action}/{id}", // URL with parameters
            defaultParameters   // Parameter defaults
            );

Note: I'd normally define enums for all the possible parameter values. Then the params can be of the appropriate enum type, and you can still use strings in your path. E.g. You could have a "Section" enum that has a "MySection" value.

Daniel Robinson
I'm using areas, an ASP.NET MVC 2 feature. So my route is like `http://localhost/Area/Controller/Action/Parameter`. Check http://haacked.com/archive/2009/10/01/asp.net-mvc-preview-2-released.aspx for more details.
Kezzer
Oh okay, thanks for that... the "teacher" becomes the "student"! Cheers :-)
Daniel Robinson