While working on an ASP.Net MVC 3 web app I found the need to have a route like this….
http://mydomain.com/ParentPortal/Adult/Edit/4
Where Edit is the command and 4 is the ID for the adult.
I ended up with the following route…
routes.MapRoute("ParentPortal", "ParentPortal/{action}/{type}/{id}",
New With {.controller = "ParentPortal", .action = "Index", .type = UrlParameter.Optional, .id = UrlParameter.Optional})
And the following action
<Authorize(Roles:="Parent")>
Public Function Adult(ByVal type As String, ByVal id As Integer?) As ActionResult
Select Case type.ToLower
Case "edit"
Throw New NotImplementedException()
Case "new"
Throw New NotImplementedException()
Case Else
Throw New NotImplementedException()
End Select
End Function
Would that be the recommended way of doing it?
Seconded for using MVC Areas. What you are doing now isn’t inline with the standard ASP.NET MVC design pattern and also relies on magic strings.
Instead, create a new Area called ‘ParentPortal’. Add to it a controller called ‘Adult’ containing Edit and New actions. Finally, register the new Area with MVC. ie:
Create ‘ParentPortal’ area by
right clicking on web project and
choosing Add->Area..
Add ‘AdultController’ controller
to the area by right clicking on the
area and choosing Add->Controller
Set authorization and add edit/new methods
4) Verify that Application_Start in your Global.asax contains: