ASP.NET MVC 2 - Organizing - c#

I'm having a ASP.NET MVC 2 Project where I'm making some management system... The thing is that I have 4-5 controllers that are all related to a specific thing in the management system and having 4-5 diffrent view folders dosent make it any easier.. Any suggestions? Since its "a part" of the management system I dont really see how I can make use of areas. All ideas welcome.
UPDATE:
For example I have a thing in my management system called "Product Management", this thing have following controllers attached:
CategoriesController =
Add/Delete/Edit categories.
ProductsController = Add/Delete/Edit
products in categories.
OrdersController = Add/Delete/Edit
orders from users.
Seems like a waste that I need to create 3 diffrent view folders for those controllers.. Would be much easier if I could create a folder under the "Views" named "Product Management" and then just create a sub folder for each controller...

I don't think you can arrange it that way.
You can create an Area as ProductManagement and have your controllers/views inside it.

You can keep the pages under Shared folder, so that they can be picked up by View engine can find and render the page.
If this is not the solution you are looking at, can you please elaborate your problem further?

Related

ASP.NET MVC: publish only selected view

I have different look and feel (not a theme - but a completely different look and feel) for my ASP.NET MVC project. My customer can select which look and feel they want and I will publish it.
So far, I split each look and feel to different projects and they share the same code. Now, the number of look and feel has increased dramatically. There are over 30 different "look and feel" option to choose from. Yes, I have 30 different projects, all codes are exactly the same (including the javascript), except the View (cshtml) and the css files.
Is there an easy way to only publish a specific view? For example, I have the following view options:
Modern
Contemporary
Classic
Unique
Andromeda
If my customer asked me that they want to use Modern "look and feel", then I will open the "Modern project" and publish it to their Azure server.
Any idea on how to use only 1 project and use something like "conditional publish"?
Thanks...
You can combine individual views in one view and control them based on an environmental variable.
For example,
Project 1:
View 1
Project 2:
View 1
You can make one separate project
Main Project:
View 1
in which you have
If(Modern)
{ View 1 Code }
else if(Contemporary)
{view 2 code}
and you can read which theme you want to load from Environmental variable. If your customer says change theme then just change the variable value.

Changing mvc projects into mvc5 area

I have for some time created a few mvc websites. The other day my super suggested I put them all into one solution as they - more or less - is related to each other.
So I have come a long way with making the projects work more or less on their own.
Yesterday I hit a little bump where I am to put one project where you log in in order to register data. I have moved the login code to the first website (front?) and now that works. It was when I wanted to create the link (ActionLink) to link to the registration project, I started doing research on moving/transforming/changing a project into an area in mvc5.
So my question:
Has anyone moved/transformed/changed a mvc5 (or 4 or 3) project into a mvc5 area?
Is there something to worry about when doing this?
There are several things to consider when merging a project.
Routes and namespacing - If you controllers that have the same names in multiple areas, you will need to add a namespace entry to the route entries. Details here http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx/
Make sure you have an area registration file that sets up each area. i usually add 1 area, then copy it's registration file and rename appropriately
Script and css dependencies - make sure that you get all those added to the root, seperate them in area folders if you like, then add them in at the correct area by using a section in your layout for dependencies
Adding a hyperlink between areas now needs to include the area name if you have conflicting controllers , you can check these answers How to specify an area name in an action link?
Think that about covers most things I have found when doing areas
Si

How to organize MVC3 project in custom folders

I’m starting to learn MVC3 and I’d like to know if it is advisable to group your views and controllers in folders different to the default ones.
So I could organize the project such as:
-->ClientsFolder
-Views
----ClientsAdmin(Folder)
------View1
------View2
------View3
----ClientInvoices(Folder)
------View1
------View2
------View3
-Controllers
----ClientsAdminController(File)
----ClientsInvoiceController(File)
-->EmployeesFolder
Etc..
Etc..
I’d like to know, if it is a common practise, how should I start to adapt the project to this structure or if somebody could point me to a tutorial which could help me started.
Thanks
What you are describing is a feature MVC 3 already has (since MVC 2 I think). Areas.
Walkthrough: Organizing an Application using Areas
UPDATE: New working link.
What you are doing is fine. There's no need for a new area for every controller, areas should be for large chunks of application that aren't really the main application (like an administration site for your main site).
This is an OK tutorial to get started MVC Movie Tutorial
ASP.NET MVC has a facility to partition Web applications into smaller units with areas.
For example you can create an admin area for the administration section.
There are Areas for arrange mvc project.
Don't invent the wheel again... =)

Creating a MVC mobile site with same model and controller as main site

I want to produce a mobile site for a client, based on their existing ASP.NET MVC 3 Azure implementation. I'm happy that I can use the existing controllers and models, but I want a new set of views, JS, style sheets, and static content, that display less of the data. I'm pretty certain I can do this with just view changes.
Is there any way in which I can achieve some sort of view switch to pickup a different set for the mobile URL m.clientname.com - or even switch them at build time?
Can I product a view only project? And link in the DLL's from the main project ?
I guess the last resort is starting a new project based on the same files, but with new views?
Any thoughts on if anyone has done this would be appreciated.
This was covered by Scott Hanselman a while ago:
http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx

Organizing MVC2 Views with a subdirectory

We have a fairly large MVC project with a large number of views and partial views (display/editor templates). Pretty much 95% of our UI is in our templates, our aspx views are just placeholders that call our ascx templates. As such, whenever we open that Views folder in VS2010, it's fairly painful to scroll down to the Shared folder for us to manage our templates.
We'd LIKE to change this hierarchy just a bit. Instead of having a folder for each controller under Views, instead we'd like to have a folder called Pages under Views and have our per-controller folders under here. This way, when we open Views we have 2 folders: Pages and Shared.
Is this easily possible without going back and editing every single one of our controllers where they simply return View(); to return View("Pages/MyControllerName/MyActionHandler");? I'd like the controllers to still simply return View(); and without my URLs changing. So in other words, I just want to move my root directory for my Views into Views/Pages, but not for templates - they stay as they are.
Solution we went with:
global.asax.cs: (add these as the first 2 lines of Application_Start() - change nothing else)
var locations = ((WebFormViewEngine)ViewEngines.Engines.First()).ViewLocationFormats.ToList().Select(x => x.ToLower().Replace("views/{", "Views/Pages/{"));
((WebFormViewEngine) ViewEngines.Engines.First()).ViewLocationFormats = locations.ToArray();
This allows our Views folder to have 3 things in it:
Pages
Shared
web.config
Since we do 95% of our UI work within our Shared DisplayTemplates and EditorTemplates, this rmeoves the mostly-ignorable stuff from being in-our-face all the time as developers. While the suggested answer was to do ViewEngines.Engines.Clear(); and then add a newly-instantiated one, we had problems with MVC playing nice with the new one. So we chose to instead alter the functional and already-instantiated instance. I don't like the casting, and we'll put some code around that to be better, but this is the functional code to get anybody started that also wants to do this.
You need to register a ViewEngine with the ViewLocationFormats property set to include your new locations.

Categories

Resources