0

In ASP.NET WebForms we have a server side file for the MasterPage. Some operations like checking the session for logged in user and reading some default data from Database are allowed in the MasterPage.

But in ASP.NET MVC, how can i have a Controller for a shared layout and using these operations?

0

2 Answers 2

2

Depending on what exactly you want to do, there are different places for these shared functions. For example, you may want to use an Action Filter and register it as a Global Action Filter for checking information about the logged-in user. Or, for reading the data from the database, you should create DAL (Data Access Layer) classes, and put them under a separate folder.

You can also create a base Controller and make the other Controllers inherit from it, or you can create a ControllerExtention class, if you want to share some code between your Controllers (an example would be the code to populate DropDownLists).

So, there's no single solution for what you want to do.

UPDATE:

Here is an example of a BaseController class:

public abstract class BaseController : Controller
{
    protected virtual void PopulateDropDownValues()
    {
        // Code for populating DropDownLists that are shared by all Views...
        // ViewBag.DropDownSelectList = selectList;
    }
} 

Then, all your Controllers or some of them can inherit form this base Controller:

public class HomeController : BaseController
{
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply,could you make an example for base Controller or filling DropDownlist?
I got it completely ... Thanks a lot :)
You're welcome. But, remember that you should only use this solution for the code that's supposed to be inside a Controller.
If the answer is useful you could've given him an up-vote for his efforts ;) I usually create a shared controller when I require dynamic actions/sections for log-in or Widgets of the page (referencing it with @Html.Action("_Breadcrumbs") etc), though there is probably a better way.
0

I usually make a base Controller class that all of my controllers inherit from, and have any validation logic in there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.