Best approach for separate styles per organisation in MVC? - c#

I'm trying to build a web application which will support multiple organisations. To keep things simple (as I don't have a lot of experience with MVC), I set up the custom routing like site.com/organisation1, site.com/organisation2 etc.
Each organisation has to be able to choose their own colors, fonts and images on their page. The structure of the page and the inner workings of the application will be the same for each organisation.
EDIT: Organisations have to be able to create and style their page on-the-fly. No developer should be involved during this process.
I'm not sure as to what is the best approach to separate the different layouts. Does anyone have a suggestion?
Thanks in advance

Try this:
Have seperate layout for each organisation with specific styles like organisation1_layout.cshtl, org2_layout.cshtml in Views/Shared folder.
2.In _Layout.cshtml, determine organisation using URL/Routing details and set layout accordingly.
#{
Layout = "~/Views/Shared/_Layout.cshtml";
if (Request.Url.ToString().Contains("Organisation1") {
Layout = "~/Views/Shared/Org1.cshtml";
}
else
{
Layout = "~/Views/Shared/Org2.cshtml";
}
}

Related

Sharing pages between different Razor Page areas

I wonder if I could get some ideas on how best to approach this.
I have a Razor Pages application. I have several areas, and each area has its own layout page and menus.
This is pretty straight forward, but I have a couple of pages I need to share with two or more areas. These shared pages will be passed an argument that indicates which area it's being used by, and I would want the layout page (menu) for that page to be the one for that area. (Ideally, the URL would also reflect the current area.)
I'm not sure if this is practical. I'd like to keep things as simple as possible. But all I come up with is some fairly complex routing stuff.
Two options I can think of:
Put the shared page in the root Pages folder and use AddPageRoute to add multiple routes to the shared page including a parameter for the area name:
options.Conventions.AddPageRoute("/SharedPage", "{areaName}/alias1");
options.Conventions.AddPageRoute("/SharedPage", "{areaName}/alias2");
You can set the layout based on the value of RouteData.Values["areaName"]. Don't use area as the parameter name. It's a reserved word as far as routing is concerned:
#{
if(RouteData.Values["areaName"] == "foo")
{
Layout = "_FooLayout";
}
if(RouteData.Values["areaName"] == "bar")
{
Layout = "_BarLayout";
}
}
Add pages to the areas that act as placeholders to generate the routes, and then extract the body of the shared page to a partial that can be placed in the top level Pages/Shared folder so that it is accessible to all other pages in the application. If you want to centralise some of the processing for the shared page, create a class that inherits from PageModel and put your processing logic there, then have the actual PageModel classes inherit from that.
You can make this by using partial views.
So, create one patial view and place it not in areas (in Common for example or other folders).
In areas folder create master view that render only your Partial view.

Use two themes in asp.net mvc

I want to use one theme for Admin Panel and one theme is a separate which I want to show my visitors. I mean to say that there should be separate themes for visitor and Administrator of a website.
How to do in asp.net mvc?
Because in mvc we have only one file _Layout.cshtml and here we have to attach only one theme.
You can create as many layout files as needed. So in your case, it is a good idea to create an admin area which has it's own layout page in which you can use your admin theme css files
Areas are logical grouping of related functionality and files needed for that (controllers,views, styles,code etc).Areas provide a way to partition a large Web app into smaller functional groupings.
For your normal visitors, their pages/views uses the default layout and for the admin users, they get the views rendered from the admin area where it has it's own layout.
For the views from admin area (or even the _Viewstart.cshtml), you can explicitly specify the admin layout
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
Define second layout in shared folder with another name like _AdminLayout etc. call the #RenderBody() function in it,
Now you can Render layouts via different ways,
1. Define layout in View:
#{
ViewBag.Title = "View_Title";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
2. Rendering layout page from ActionResult (using Controller. View extension method):
public ActionResult Action_Result_Name()
{
return View("Action_Result_Name","_AdminLayout");
}
3. Using _ViewStart.cshtml Page:
_ViewStart.cshtml page used to define the default layout page for the MVC application.
#{
layout = "~/Views/Shared/_AdminLayout.cshtml";
}
If you are using Identity framework then you can define layouts on the basis of conditions as well
#{
if (User.IsInRole("Admin"))
{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}

Custom layout for website back-end with MVC

I have a website with a front-end dependent on the _Layout file in MVC for navigation but it is not applicable in the back-end so I would like to use a custom Layout file for this.
I have seen this used often.
Layout = ViewData["~/Views/Shared/_AdminShared.cshtml"];
It would be placed in the view, this, however did not work for me.
Are there any better alternatives? Why does this solution not work?
_AdminShared.cshtml just contains the default _Layout content found on a new MVC project (for now)
You can directly use your custom layout by doing this without the ViewData:
#{
ViewBag.Title = "CustomLayout";
Layout = "~/Views/Shared/_AdminShared.cshtml";
}
Make sure that the layout is really existing under the Shared folder.
You can also directly declare it in your Controller specific to your desired View:
return View("Index", "~/Views/Shared/_AdminShared.cshtml", Model);

Cant i ADD another Layout which is other than default Layout in MVC4

I am Working On MVC4 architecture. I have a default layout i.e _Layout and models and view associated with it. It's Working fine.
Now Due to Some Requirement, after i Click on specific link i need it to redirect it a Whole different Layout and there i will associate models and view according to my Need.
But How to add a new layout and also keep the existing default layout in place.
For ex :- On Index page i gave a link Nikhil</a> -->. When i click this i have to go to a seperate layout and view.
Please Help in simplest and descriptive way possible because i am new to MVC4.
Thanks In Advance.
Yes, you can create multiple layout for different views if you want. In my case I have AdminLayout and UserLayout in my Shared folder. Just replace Layout variable.
Ex.
// For Admin Pages
#{
Layout = "~/Views/Shared/AdminLayout.cshtml";
}
//For User Pages
#{
Layout = "~/Views/Shared/UserLayout.cshtml";
}
By the way I use razor view engine here.
For Navigation you can create an ActionLink using HTML Heplers on your index page Like:
#Html.ActionLink("Link Text", "Controller","Action")
Now when you click on this link , the action specified of the controller will be accessed and in the action you can return the desired view, which has the different layout like :
public ActionResult Login()
{
return View("Desired View Name");
}

Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".
I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?
I already tried rendering it in the partial like so, but that didn't work.
#section ScriptContent {
... content ...
}
It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.
To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.
If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.
P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

Categories

Resources