Group controllers and view in folders under the /Controllers and /Views folders

I'm working on an application that has a few different parts, so I want to have urls like this:

  • /PartA/controller/action
  • /PartB/controller/action
I would like create folders named PartA and PartB under the Controllers and Views folders. I also want them to be in their own namespaces. What kind of routes should I add for this so they are found with the above urls?

Thanks!
0
random0xff
12/19/2007 12:21:14 PM
asp.net.mvc 4300 articles. 0 followers. Follow

4 Replies
839 Views

Similar Articles

[PageSpeed] 38

Hi random0xff,

You can put your Controller classes in any folders or namespaces you like. When looking for the controller "Blah", the framework will scan all your referenced assemblies for a class called BlahController (derived from Controller), and will find it no matter what folder or namespace it's in. You should avoid having more than one class called BlahController, even if they're in different namespaces. So, you can use a route entries like:

 

RouteTable.Routes.Add(new Route
{
Url = "[Part]/[controller]/[action]/[id]",
Defaults = new { action = "Index", id = (string)null },
RouteHandler = typeof(MvcRouteHandler)
});

RouteTable.Routes.Add(new Route
{
Url = "Default.aspx",
Defaults = new { controller = "Something", action = "Index", id = (string)null },
RouteHandler = typeof(MvcRouteHandler)
});
  

The first entry there will match both URLs you mentioned. The [Part] bit will just be ignored, since the framework can find your controller anyway. 

By default, it will expect any views referenced by BlahController to be in the folder ~/Views/Blah/ or ~/Views/Shared/, but you can actually put the view in any folder you like, and reference it with a full path, like this:

  

[ControllerAction]
public void Index()
{
RenderView("~/Views/AnyFolderName/Index.aspx");
}

Thus, you can arrange any folder structure you like.
 


 



http://blog.codeville.net/
0
SteveSanderson
12/19/2007 1:41:25 PM

SteveSanderson:
The first entry there will match both URLs you mentioned. The [Part] bit will just be ignored, since the framework can find your controller anyway. 

It will also match /Goo/Controller/Action which is probably not what you want, and creating urls using the helper methods will not know what to put in [part] unless you specify it every time which gets pretty annoying.

 

So say you want the following routes:

  • admin/Users/Index
  • admin/Logs/Index
  • users/Dashsboard/Index
  • users/Profile/1/edit

There are 4 controllers and two areas, so we need to generate two specific rules for each area, or 4, 1 for each controller.

RouteTable.Routes.Add(new Route
{
Url = "admin/[controller]/[action]/[id]",
Defaults = new { action = "Index", id = (string)null },
Validation = new { controller = "[uU][sS][eE][rR][sS]|[lL][oO][gG][sS]" }
RouteHandler = typeof(MvcRouteHandler)
});

RouteTable.Routes.Add(new Route
{
Url = "admin/[controller]/[action]/[id]",
Defaults = new { action = "Index", id = (string)null },
Validation = new { controller = "[dD][aA][sS][hH][bB][oO][aA][rD]|[pP][rR][oO][fF][iI][lL][eE]" }
RouteHandler = typeof(MvcRouteHandler)
});
 

 So that is pretty ugly... what is going on.  We create two routes, one for each area, the areas are hard coded so we do not allow accessing goo/controller/action.  We slap a Validation expression on the controller so it will automatically generate a url under the correct area, and it will not allow a controller to be accessible from the wrong area.  Why does the validation look so weird.  Well, the url is case insensitive but the validation, which takes a regular expression is not.  The expression I used basically gives case insensitivity.  If there is a better way please let me know? 

Please note, without the validation every time you tried to create a URL for the Profile controller it would end up prefixed with /admin/ because that would be the first rule matched.

The other alternative is have a rule for each area and controller so you don't need any validation.  In this case the Url would be admin/Users/[action]/[id] and then include controller="users" in the defaults and drop the Validation.

I have some solutions to this problem that I am working on which are much more elegant.  I am working on a route builder to be able to use either Namespaces as automatic area partitions and/or an AreaAttribute you can add to the controller to specify the area.  The builder will handle making all the necessary routes and parameters in the correct order with the correct validation.  It should be in MVC Contrib next week.

My biggest problem is how slow matching routes and generating urls is.  The algorithms in the framework are not optimized, yes I know its CTP, and there is some locking that happens which prevents matching and generarting more than one url at a time.  Without these things fixed my solution gets pretty slow for large apps with many areas because as you can see there is some "Rule Explosion" that has to happen to keep everything working under the covers.

 


Adam Tybor -- abombss.com
0
abombss
12/19/2007 5:50:50 PM

Great, thanks for the answers! I have no idea what the bracket notation is or how it works, are some word 'magic' and others not? If so, I'd love to have [namespace] and/or [folder] but that's based on my very limited experience, so it might not be practical at all. I do remember scottgu saying that the team was thinking about using an extra folder inside Controllers so it would be easier to combine different parts without controllers colliding because they have the same name. Of course, the folder/namespace would have to work automagically in methods such as RenderView, so if the controller is in folder or namespace X, it will look there for the view as well.

I can imagine routes not being very fast if the strings have to be inspected and all, but that should only happen on startup right? I can't wait for the next release to see this evolve.

Thanks! 

0
random0xff
12/19/2007 8:14:36 PM

random0xff:
I have no idea what the bracket notation is or how it works, are some word 'magic' and others not?
 

The ones in brackets are parameters.  By default they mean any string, almost like "*" wildcard.  The name of the parameter gets mapped and bound to the name of the parameter in your action method.  You can fill in default parameter values for these in Defaults section of the route, and you can add Validation to the parameter using a regular expression in the Validation property of the route.  ScottGu has a great post on routing and how this works.  The magic words are [controller] and [action].  A value for these two parameters must be supplied otherwise the default MVCRouteHandler and ControllerFactory will bomb.  There is also one other magic word you can use in Validation which is Method, that matches the HttpMethod used to make the request.

random0xff:
If so, I'd love to have [namespace] and/or [folder] but that's based on my very limited experience, so it might not be practical at all.

I don't think you want this.  MS MVC needs to support areas and controllers with the same in some way which would fix this.

random0xff:
I can imagine routes not being very fast if the strings have to be inspected and all, but that should only happen on startup right?

Actually at startup its pretty fast, there is not much reflection taking place.  The issue is with matching an incoming url and generating the urls.  When the RoutingModule makes a call to GetRouteData to match a route, the RouteCollection locks the routes.  Now new requests coming in or Views rendering links have to sit a wait for the lock to release.  Meanwhile while RouteCollection is attempting to match a route it enumerates each route in the order it was added.  It then attempts to build as much as possible until it fails.  Once it fails it moves on to the next one.

Some type of binary search tree would be much faster at finding possible matches instead of enumerating every route.  The locks should also be swapped with Reader Writer locks as you will have far more reads than writes and it is very inefficient to block on reads when nothing is writing.
 


Adam Tybor -- abombss.com
0
abombss
12/19/2007 9:18:10 PM
Reply:

Similar Artilces:

A View Folder inside a View Folder
Hi everyone! I have this situation: Views/Admin/Scheluder/Index.aspx, it means I have a folder "Scheduler" inside a folder "Admin" and so on. On the other hand, I have a View/Register/Finalize.aspx and this page has a button and on its onclick event I wrote: "window.location.href('/Admin/Scheduler/Index')" But when I click on it, it throws an exception. So, how can I call this View "Scheduler" that is inside another View folder (Admin)??Thaaanks!!!   Hi, when you are specifying a link then you always specify link to action, not t...

How to hook up jQuery + MVC View User Control / MVC View ?
Hi folks,   If i have an MVC View User Control or a standard MVC View, how do i hook up some jquery specific to those parts/pages? For example. Image we have a list of northwind products and when we click on an item in the list, it displays the item above/underneath/somewhere near the list, via an ajax call which returns some html. Now, i'm not sure how to wire up the onclick events and where the javascript code is suppose to go? a seperate file? if so, how is the method 'wired' or 'hooked' up? All the examples i see with jQuery say that you just make the...

Problem with view control redirecting to another view control
I have a module with an ascx view control and a second ascx view control with key MakeTest When the user  clicks in a hyperlink i'm using this in order to redirect him to the other view control: Response.Redirect(Globals.NavigateURL(TabId, "MakeTest", "mid=" + ModuleId)); This works, but when you're on the second view control (MakeTest) the other modules from the same tab dissapears as when you're in edit mode Can't this be avoided somehow? I have a banners module at the top in all pages, but it's very sad to see it dissapear when i'm still in view mode. Please help me,Thanks BT...

Can't drag any control INTO a view control... places new control at top right of design view.
VS 2005 created new web site.On default.aspx design view I added a Multiview control. I then added a View control within that.Here's the problem.  I can't place any controls inside of the view control.  If I drag a new button from the toolbox into the view control that I have in place, the button ends up in the top left of the design view. If I then Cut and paste the button into the View control, the button still ends up outside of the view control in the top left corner of the design view. What's causing this?  I have seen this done in many of the w...

navigate from Edit control back to the view control when the view control needs parameter in the url ( ver 2.1.2)
Hi, I am writing a module for 2.1.2. The view control is loaded based on a "&itemId=" parameter appended to the url. The module has Edit and Add controls. I use the same ascx page for both (register the same page twice as Add and Edit). Navigation to the Edit is done by using EditUrl("itemId", itemId, "Edit") and navigation to the Add is done by using EditUrl("itemId", -1, "Add"). This goes well. The problem is going back from both Edit and Add to the view, when clicking update, delete or cancel. I've tried EditUrl("itemId",itemId, "") going back from the Edit, but I get the "mid=...

Is there a way to group controls (textbox, dropdown, etc) in a view of MultiView so I can disable the group of controls?
I have a MultiView with 5 different views.Each view has 5-10 controls consisting of TextBoxes and DropDownLists. As I hit a button to go from a view to another, I want to disable all the controls on the previous view so the end user can't go back to the previous view using his backspace key or Back button on his browser.  I can achieve my objective by going through each control and setting is Enabled = false, but it would be really great if there is a way to put the controls into a group and simply disable the group versus 5-10 controls.Example:protected void SectionOneSub...

superreview requested: [Bug 362741] Selecting folder with previous view unread, while viewing another unread folder, fails. : [Attachment 247567] Remove optimizations when view is unchanged
Karsten D�sterloh <mnyromyr@tprac.de> has asked Scott MacGregor <mscott@mozilla.org> for superreview: Bug 362741: Selecting folder with previous view unread, while viewing another unread folder, fails. https://bugzilla.mozilla.org/show_bug.cgi?id=362741 Attachment 247567: Remove optimizations when view is unchanged https://bugzilla.mozilla.org/attachment.cgi?id=247567&action=edit ------- Additional Comments from Karsten D�sterloh <mnyromyr@tprac.de> > var result = GetMailViewForFolder(msgFolder); ... >+ if (document.getElementById("mailvie...

superreview cancelled: [Bug 362741] Selecting folder with previous view unread, while viewing another unread folder, fails. : [Attachment 247567] Remove optimizations when view is unchanged
Kent James <kent@caspia.com> has cancelled Karsten D�sterloh <mnyromyr@tprac.de>'s request for superreview: Bug 362741: Selecting folder with previous view unread, while viewing another unread folder, fails. https://bugzilla.mozilla.org/show_bug.cgi?id=362741 Attachment 247567: Remove optimizations when view is unchanged https://bugzilla.mozilla.org/attachment.cgi?id=247567&action=edit ...

Tree view control for file folders
Hi there, I am using the IEWebControl and trying to get the folder structure from a PC to the web. I have the logical drives displayed correctly. (A, C, D...) but when I try to get the next level child nodes, how do I get the Expand (+) in the folder list if it has child in it. For example c:\Dell is a folder which has subfolders. I need to display Expand but not all the folders in it. Is there a way to find the IsThisFolder? I am posting everytime the user expands the folders, is that the right way? Any help would be greatly appreciated. If anyone could give me sites that have source ...

Change Folder in MS Outlook View Control
PB 10.5.1. Put a MS Office Outlook View Control on a window. In comes up with the Inbox showing. How to change to another folder? Should be something like "something.Folder = 'Tasks' But can't discover what that 'something' is. TIA KL Try object. <KL> wrote in message news:47c7262a.15fb.1681692777@sybase.com... > PB 10.5.1. > Put a MS Office Outlook View Control on a window. > In comes up with the Inbox showing. > How to change to another folder? > Should be something like "something.Folder = 'Tasks' > But ca...

Wizard Control
Hi All, I have simple wizard control. The first step contains checkboxes and depending upon which checkboxes were selected, I want hide (skip) steps in wizard control. I am using <code>ReportDataWizard.WizardSteps[1].Visible = true</code>  but getting following error. Please help.  The Visible property of a View control can only be set by setting the active View of a MultiView. Hi,See this quickstart tutorial http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/standard/wizard.aspx Specifically look at the section Wizard NonLinear Navigationand Sc...

Controllers in class libraries and change Views base folder
Hi, I'm new to MVC and I'm studying it to see if I can use it in my company for a specific project. I've been searching but couldn't find anything relevant. Maybe the search terms aren't correct. Is it possible to have controllers in a class library that is referenced by the Web app project? The idea is to reuse controllers among several web sites. I guess I could do it with some custom route, by can't find the right place to start. How can we change the folder where the views are located? If I got this right, the views are always located under the Views folder....

need some feedback on using a multiview control that displays data in a formview control on the fourth view after making selections with radiobuttons on the previous views
I need some feedback on using a multiview control that displays data in a formview control on the fourth view after making selections with radiobuttons on the previous views. I am designing a webpage that allows the user to make certain selections with radio buttons that displays information about the selections on the last view but it seems the web page may be storing there selections in a cache or something because sometimes the last view wont change when I make a different selection with the radiobutton but for the most it changes upon selection but sometimes it wont view the changes...

superreview requested: [Bug 346747] Add Rename and Delete Support to Folder Views : [Attachment 231528] fix for deleting local folders from the favorites view
Scott MacGregor <mscott@mozilla.org> has asked David Bienvenu <bienvenu@nventure.com> for superreview: Bug 346747: Add Rename and Delete Support to Folder Views https://bugzilla.mozilla.org/show_bug.cgi?id=346747 Attachment 231528: fix for deleting local folders from the favorites view https://bugzilla.mozilla.org/attachment.cgi?id=231528&action=edit ------- Additional Comments from Scott MacGregor <mscott@mozilla.org> This seemed like the best spot to do this, let me know if you disagree. When deleting a local folder to the Trash folder, we want to clear the ...

Web resources about - Group controllers and view in folders under the /Controllers and /Views folders - asp.net.mvc

Resources last updated: 11/26/2015 7:41:38 AM