tags:

views:

63

answers:

1

hi there

i usually create asp.net websites and have a few classes i use, mainly a baseclass normally i would change

public partial class _default : Page

to public partial class _default : BaseClass

with a using at the top of the namespace name

however i dont seem to be able to do this in MVC, how do i get my baseclass or any other class into my mvc page.

the reason i ask is that i have a class called errors, i usually have this in my global.asax file:

void Application_Error(object sender, EventArgs e) 
{
    Exception objErr = Server.GetLastError().GetBaseException();
    namespace.errors.WriteError(Request.Url.ToString(), objErr.Message);
}

but i seem unable to do this, even with a shared namespace across the whole site

it is most probable that this question is lame but what the hell :-)

ta

+1  A: 

For the base class You can create ApplicationControler Somethig like :

namespace YourApplication.Controllers
{
    public abstract class ApplicationController : Controller
    {
        public ApplicationController()
        {
            using(ApplicationDataContext menu = new ApplicationDataContext())
            {
                // loading data for menu control
                MenuRepository myMenu = new MenuRepository();
                ViewData["menu"] = myMenu.MenuList();
            }
        }
    }
}

And then you can just extend it from any controler like this:

namespace YourApplication.Controllers
{
    public class DefaultController : ApplicationController
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

Notice in my example in application controler I am loading view that I am gonna display on every single page, and you can load anything you want, anything that you need often or always. Thats pretty much your base class.

Dmitris
thanks very much, thats quite a nice idea, there is quite a lot to learn really fast for the MVC stuff and this will be sure to help alot
minus4
Yes, it is quite nice idea to have application controler, in case of this example I am actually displaying the ViewData["menu"] in the master page trough mvc user control (partial). And you of course can have variables and what not.
Dmitris