ASP.NET MVC: publish only selected view - c#

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.

Related

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)

Share PartialView between projects in a solution?

I am developing 2 MVC5 websites. They run seperately, but actually related to each other (1 Admin site, and 1 User site). In both site, I have to render a graphical board using HTML table. I am using PartialView with a Model to render it.
Currently, this is my solution structure:
Project.Common (A class library project, contain the model)
Project.Admin (MVC5 project for the Admin site), refering to Project.Common.
Project.User (MVC5 project for User site), refering to Project.Common.
To render the graphical board, I will use BoardData class from Project.Common, which is ok. But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later.
Please suggest a good solution for reusing the PartialView. I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. Is there any way to do this within Razor View?
One of the options you can use is add a link to a file. In this case keep your Partial view in just single project and in second project use Add -> Existing Item, select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link. More info can be found on MSDN, see section Add an existing item as a link.
In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well.
You should embed your views into the Project.Common dll, a detailed description can be found on https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6

How to specify different AddController code templates for different MVC 3 Areas?

I know how to customize a controller/view template for an MVC project. However, I have a few different MVC 3 Areas in my project and each Area has its own Models, Views and Controllers. Is there a way to specify different code templates for each Area? I tried to place a CodeTemplates folder in one of my Area sub folders but it didn't work.
The idea is, when I right click on any Controller folder in an Area and select Add -> Controller, I want it to use that Area controller template.
UPDATE:
I will be happy with being able to select the controller template I want to use, but still wondering if it is possible to specify CodeTemplates for the different areas.
Looks like MVC Scaffolding might be my only option here. I will show specific solution when I get it working.
Rereading you question, I realized my answer may not be what you are trying to do. Do you wan't the auto generated code in the view to use a different template? Or did you wan't the page to look differently, like with css changes, etc? My answer applies to the latter. If you want to use different code templates, it seems like (according to this) as long as you give them different names you should be able to select the one you wan't to use. You could also probably just remove the ones you don't want from the project then add them back later.
Create 2 files in the folder you want the template to be applied to, one named _ViewStart.cshtml and the other _Layout.cshtml
In the _ViewStart file, add this:
#{
Layout = "~/Views/Reports/_Layout.cshtml";
}
This will point to the _Layout so change the path as needed. In the _Layout file add whatever you want, just like the layout in the Shared directory. And for any other folder you want to use this layout, just add the _ViewStart file to that directory and point it to the _Layout file you want.

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