Use two themes in asp.net mvc - c#

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";
}
}

Related

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);

How to include style sheet only on pages in a certain Area in ASP.NET MVC?

I have a style sheet that I only want included on web pages that are a part of a specific Area in MVC.
I know, by default, you get Areas/<Area Name>/Views/_ViewStart.cshtml, but I want the default layout rendered for this area. My Area's _ViewStart file looks like:
# {
Layout = "~/Views/Shared/_Layout.cshtml";
}
I tried adding a call to Styles.Render("My Bundle Name") in the _ViewStart file for my Area, but the <link> tag is not showing up in the HTML source.
I even created an HtmlHelper extension method that returns the name of the current Area in MVC from the Route, and including the stylesheet that way:
#if (Html.AreaName() == "MyArea")
{
#Styles.Render("...")
}
Code for the AreaName() extension method:
public static string AreaName<TModel>(this HtmlHelper<TModel> htmlHelper)
{
return htmlHelper.ViewContext.RouteData.Values["area"] as string;
}
But this always returns null. I even debugged the application and poked around the "Locals" debugger tab. The route data contains the controller, id and action name, but not the area name even though it's rendering a page in my area.
Background
I am creating a NuGet package for this area that can be installed on any MVC application. In our organization, we are given a basic HTML and CSS layout based on Bootstrap, so there is some predictability with layout, but I'd like to hedge my bets and only import styles specific to my Area on pages rendered from that Area.
How can I include a style sheet for pages in one certain Area in MVC, that doesn't show up in other Areas, or pages with no Area?
You should read the area name from DataTokens
This should work.
public static string AreaName<TModel>(this HtmlHelper<TModel> htmlHelper)
{
return htmlHelper.ViewContext.RouteData.DataTokens["area"] as string;
}
It's not an elegant solution, but you could set a PageData value in your ViewStart and then check for it in the main Layout page:
_ViewStart.cshtml for your area
#{
Layout = "~/Views/Shared/_Layout.cshtml";
PageData["AdditionalStyleBundle"] = "~/Content/myCSSBundle";
}
_Layout.cshtml
<head>
#if (PageData.ContainsKey("AdditionalStyleBundle"))
{
#Styles.Render(PageData["AdditionalStyleBundle"]);
}
</head>

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");
}

Best approach for separate styles per organisation in MVC?

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";
}
}

Trying to use two layout pages on MVC4 and Razor

When I use #RenderBody on a view (not principal), I receive this message Error: The file "~/Views/Shared/_Sistema.cshtml" cannot be requested directly because it calls the "RenderBody" method.
I do not understand it as I am a novice in MVC.
What do I can do?
Thanks!
If you are using the Renderbody in _Sistema.cshtml file, then make it as a Layout page.
And add another partial page named like MyPartial.cshtml with Layout name as _Sistema.cshtml.
Renderbody is supposed to be in the master page only. i.e., Layout page.
So your _Sistema.cshtml page should only contains the following:
#RenderBody()
#RenderSection("scripts", required: false) #*----Optional---*#
Then your your new partial page MyPartial.cshtml should contain the following:
#{
Layout = "~/_Sistema.cshtml";
}
Then use your partial page in your view as follows:
#Html.Partial("MyPartial")
Hope it helps.
RenderBody is for masters only. This method renders markup of content pages that does not belong to any particular section. If your view calls RenderBody, two cases are possible:
Either this is a mistake and this view should not call it.
Or this view is a master, and you should instead use some other views inheriting layout from this master.
you just need to interact with _ViewStart.cshtml
and use if condition to specify the share mater page for each group of users.
for example user is admin then user _Layout.cshtm other wise use _Layout2.cshtml
use following code :
#{
if(User.Identity.Name.ToLower()=="admin") {
Layout = "~/Views/Shared/_Layout2.cshtml";
}
else {
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
In my experience, I was struggling with this same issue for days. After further investigation, I found that when starting a new Umbraco site, I had the option to select templates. That resolved the RenderBody issue. When creating the layout page without the templates it doesn't see the Master page as the layout, hence not being able to call the RenderBody method. Using the templates automatically sets the Master page as the Layout allowing you to call the RenderBody. I hope this helps.

Categories

Resources