How to implement logic at "masterpage" level - c#

I'm still new to MVC, so bear with me :-)
I've got a community site I'm working on, and I'd like to show how many users are online on all my pages after the user's been logged in.
I've got a shared view which is used as layout for all pages after login (UserLayout.cshtml)
Can I somehow add the logic to show online count to my shared layout ?
If it were WebForms I'd just have some code-behind for my masterpage, but this is obviously not an option here.
The information about users online is fetched from a cache. It's not available as a property on any of my View Models.

You can write an action which renders the information (using a very small view)
You can then call Html.Action to render it from the layout page.

You can create a 'UserLayoutModel' class and have all other view models derive from it. You can also use 'RenderAction' to have a part of the UI rendered separately (make sure you mark this action with ChildActionOnly attribute).

What I did was create a BaseController.cs that all controllers inherit from, and in the base controller you can override OnActionExecuting and any viewdata values you set here will be available to your master page.
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
base.OnActionExecuting(filterContext);
}

You can create a Global Action Filter.
Normally you add an Action Filter as an attribute to a method or class ([HttpPost]). Using a global Action Filter you can add code to every Action, without the need to inherit from a specific class. It is like you added an attribute to each and every Action method.
This article explains a lot.

Related

ASP.NET Core 6 How to redirect from one view to other

tl:dr:
I would love to redirect from my Layout view into an index view of a certain class. I do not want to do it through a button.
After I log in the layout page changes depending on your role. If you have a regular user role I want you to be immediately redirected to another view.
This the part of the code where I would like the redirecting to take place. In the "else" clause.
Is there a simple way about please?
I was thinkig of calling a method from a controller of said class which would simply have the redirect method inside. But that has failed since I was not able to call such method simply in a view and I failed to find a way after a long google search.
Thank you for your help.
According to your description, if you want to redirect the view from layout to another view, I suggest you could consider using the Context.Response.Redirect("~/Account/LogIn");
More details ,you could refer to below example:
#if (Context.User.Identity.IsAuthenticated)
{
}else
{
Context.Response.Redirect("/home/Privacy");
}

Accessing a method in a BaseController from a Layout View in MVC4

In my MVC4 web application I have both a GlobalController which all controllers will inherit from so I have a single place that has all the common code that must run on each and every page. I also have a _MasterLayout.cshtml page that all my views use as there layout page. So far I have been able to put code in my GlobalController to fill ViewBag data with stuff to propagate dynamic data to the _MasterLayout.cshtml file.
I now need to figure out how to place buttons and/or links that people can click on to do things like register, login, logout, etc. As you could imagine these functions could be used on any page in my site so I would like the code for those to live in my GlobalController. I have already created public classes inside the GlobalController to do the actions I want but what I can not figure out is how to wire up a click on either a link or button placed on the _MasterLayout.cshtml file to the GlobalController public class?
I DO NOT WANT TO RENDER NEW VIEWS!
Your BaseController should only contain methods that controllers need to know how to do, like finding Views hence the View() method. Then every other controller should take care of their own jobs. So for Login, Logout, Register those all deal with account management. So you would create an account controller and put those actions inside
public class AccountController: BaseController {
public ActionResult Logout() {
/* logout user */
}
.....
}
And then in your views you would just create an action link to to what you need.
<div id="header">
#Html.ActionLink("Log me out", "Logout", "Account");
</div>
or you can call it directly
<div id="header">
Log me out
</div>
Hope this helps get you on the right track. I would also suggest going through the Music Store Tutorial for a better idea of working with MVC.

Custom controller for the layout.cshtml

I have an MVC website which I wanted to implement a globalization/localization. On my _Layout.cshtml, I have a dropdown which have the languages I supported. When a user selected a language on that dropdown, it should automatically post to the server then render the new language.
Is it possible that I create a specific custom controller for the _Layout.cshtml only? If yes, how? If no, is there any possible way or approach I can make?
Thanks in advance!
You should move that to a child action, then call the child action from the layout view.
You can make the form POST to a separate action (in a controller shared with the child action) that sets the cookie / session / DB property, then redirects back to the original URL (via Request.UrlReferrer or from a hidden model-bound field).

Should view render menus or master pages

I've developed my own custom users and roles objects using ActiveRecord that do NOT extend the default Asp.Net providers and therefore I can't get the user from the HttpContext. I can create a custom htmlhelper to render menus but should my views render the menu or the master page?
If it's the master page how can I pass to the custom htmlhelper things like current user since some menu items depend on the user roles.
Also, how can I detect what controller is being viewed inside my master pages?
1) If your menu functionality is supposed to exist on multiple pages, then it makes sense to put it in the master page. If not, then the normal view.
2) A popular choice is to make all of your ViewModels inherit from a base view class, and then your Master page uses that. Example:
System.Web.Mvc.ViewMasterPage<ViewBase>
System.Web.Mvc.ViewPage<MyViewModel>
public class MyViewModel : ViewBase { }
3) You can pull out the specific controller from the route data. However, if you need specific functionality for certain controllers, I would just suggest using a different master page for those views than trying to make all of your views use the same master page.
In general, all ASP.NET controls (whether WebForms or MVC) should control their own state.
In the case of handling navigation, I'd say create a .ASCX (partial view) and place it on your master page. Let the partial view control how it is displayed based on the HttpContext.

C# Handle GUI in MVC

I am using the asp.net MVC Framework. IN my application a user has to log in. And when the combination of username and password is correct, the div (or panel?) with with the menu in it, must become visible. But how can I do this? When a name my panel pnlMenu, in my controller i cannot do something like:
pnlMenu.visible = true;
So, how do i have to do this?
What you should do is in your controller check to see if a user is logged in and set a value in the ViewData like this:
ViewData["IsLoggedIn"] = true;
Then in your view you can set the visibility of the method based on this value. This way if you change the view later, or decide to have multiple views, they can each use this value and there isn't any coupling between your view and your controller.
Create a method or property on your View that enables you to hide or show the appropriate controls ?
Then, in your Controller you can access that property or method of your View, can't you ?
You do not want to reference specific 'controls' on your View in your controller, since, one of the ideas of MVC is that you can just replace the UI with another implementation (web / win / ...) and make use of the same controllers and application logic.
Then, you just want to describe an operation that your View should support, so, in the interface that describes the 'contract' that your View must support, you should create a method which is called 'ChangeState( bool loggedIn )' for instance.
In the controller, you can call this method when the user has logged in.

Categories

Resources