How to organize a .NET MVC application in Visual Studio - c#

I am starting to create a complete application in Visual Studio (.NET MVC). Does it makes sense if I put everything into a single solution and create three separate projects: Admin (used by staff to manage the app data), Web (public facing web pages), Shared (common data access and other code). This seems to make sense as later I might add a project for Mobile or an API or ...
If not, how do you structure your solutions/projects in Visual Studio?

If it were me, I'd have everything in one project and use areas to separate my views and models.
Example,
Project
Areas
Admin
Controllers
AdminController.cs
Models
Admin.cs
Main
Controllers
MainController.cs
Models
Main.cs
LoginLogout.cs
LoginForm.cs
Views
Admin
Shared
permissions.cshtml
admin.cshtml
Main
Shared
loginLogout.cshtml
loginForm.cshtml
master.cshtml
Index.cshtml
Walkthrough: Organizing an ASP.NET MVC Application using Areas
Areas provides a really nice way to section off pieces of your web application, and because it's all in the same project you can have all your sections use the same View Layout (Master Page) to make it all look the same from section to section.
Areas will map to urls like
http://somesite/main/index
http://somesite/admin/manage/index
In that example, you would not put your main section in an area, instead use normal model view controller patterns for the main part of the site, and only introduce an area for admin or other sections.

Related

Webforms in class library in asp.net

I am making a class library in asp.net , is there any possibility to add web forms in class library ( content view files with their code-behind ) so i can run , only with a simple method in the CALLER PROJECT ,a whole web app (thanks to this class library) .
I searched and i found i can't but If not , is there any near solution which can solve this problem ?
Thanks !
is there any possibility to make a class library as a web app "template" that we can use every time we call it in another project
A "class library" project, in Visual Studio terms, is strictly namespaces and classes - no web forms or controls allowed. And you're right, these projects are easily shared among sites by leaving them in a common source control repository or similar.
In an "ASP.NET Web Forms Web Application" project, you can reduce duplication by moving as much "code behind" code as possible to class libraries, which can then be shared among your team(s). You could even theoretically implement a lot of page/control functionality as "base classes" stored in class libraries and then inherited in aspx.cs and master.cs "code behind" files.
You could also move as much "code front" as possible into Custom User Controls which could be shared across projects by linking source control to a common repository.
But I don't think there's a good way to "share" .master and .aspx files. You could set up a basic website, keep it in a common place, and let people copy/paste it for new projects. But I don't know of a useful way to share that UI code among multiple web app projects.

Modularised ASP.NET MVC web site

I'm currently refactoring a huge ASP.NET MVC website. The main idea of what I'm trying to achieve is having a main (front) website and several separate websites in the same solution that can be deployed separately. The reason for that is that we have different teams working on a different project.
The current setup looks like this:
Main website ASP.NET MVC
Module 1 (Area)
Module 2 (Area)
other modules..
Common Business Layer
Common Data Layer
Common View Layer
When making changes to a module, you have to deploy the main website (so although Areas are acting as separate projects, they cannot be deployed separately). Areas are using RazorGenerator for the views. The Common View Layer generates Embedded Resources to be used in the main website/modules.
Any solution for that?
What I'm trying to accomplish right now is extracting the modules from Areas into full ASP.NET MVC websites and map them in IIS as subdomains to the main website. The problem here is that I have repeating views, styles, layouts in all projects and making a change to the main menu, style, etc. would mean going through all the projects and make that same change. Not a viable option.
Thanks for the ideas!
So the answer can depend also whether you want to have several solutions (*.sln) or it will be just one solution with several mvc projects.
Ad.1 Multiple sln / directories.
In that case probably interesting option is to configure virtual directory with some static content like styles, scripts etc. Such virtual directory can be quite easily configured on IIS: https://msdn.microsoft.com/en-us/library/bb763173.aspx
Ad.2 One solution, multiple MVC projects:
Then I'd suggest to add some solution folder like 'Common Presentation Layer Files' -> then I'd move all those files to be shared into that solution folder. Next in those MVC projects I would do following thing => right click => add existing item => as a link.
As the result you'll have a copies / shortcuts to the original files. You should do the updates only in that common folder and the changes will be visible in all other mvc projects. https://msdn.microsoft.com/pl-pl/library/9f4t9t92(v=vs.90).aspx Then you can publish your MVC projects separately and if properties of those static files are ok all the files will be copied during publish. So the structure would look like:
Solution:
MVC App 1 (proj)
MVC App 2 (proj)
'Common Presentation Layer Files' (solution folder)
other projects (projs)

How can one develop a rapid prototype for a web site while still using ASP.NET MVC?

I am developing a website in which I want to use ASP.NET MVC 5 because the site has the potential to be quite large and require the separation of concerns that MVC provides. However, the client wants me to show him a "prototype" of the home page which is simplay a single page that shows an image, some text, and requires a user login (and he wants to see it ASAP). How can I reconcile the fact that developing a proper ASP.NET MVC site requries a large amount of initial time to set up with my requirement to quickly put together a page to show my client what he has in mind? Should Also, should I consider using Web Forms over Razor for this initial part of the site?
You can prototype it fast just by modifying default view, without creating models controllers etc.
Login functionallity exists out of the box in MVC.
You can download one of MVC templates from Visual Studio.
Select File > New Project > Online > Templates.
You can find them also here (most of them are kept are up to date): https://visualstudiogallery.msdn.microsoft.com/site/search?query=mvc&f[1].Value=mvc&f[1].Type=SearchText&f[0].Value=templates&f[0].Type=RootCategory&ac=4
The Visual Studio Single Page Application template is super quick to setup. It'll run right out of the box. Just add your image and text to the Home view.

Using a project instead of an area in MVC3

I know how to use areas in MVC3 - and I think it works very well, but I really want to make a higher level of seperation in my project.
I have multiple projects in an empty solution, a Domain project (containing my entities, persistence etc.) - a Framework project with tools, helpers etc.) and a UI project which right now contains the frontend and an area with the /admin for the solution.
Because a want to have a more clean solution for the UI and the Admin, I want to seperate these into two seperate projects.
How can I route the URL?
The url / is pointing at the frontend project.
The url /admin is pointing af the backend project.
Your /admin is nothing more than another virtual directory mapped to your admin app. It should work just fine like that, as it's how iis and asp.net works.

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... =)

Categories

Resources