0

I am new in how to used MVC Pattern in asp.net. In general web Application I have initialize configuration which are common on website level

Like,

public partial class Default : MyBaseClass
{
}

public class MyBaseClass : System.Web.UI.Page
{

 public override OnDo()
{

}
}

Please ignore if any spell mistake. In OnDo() function I initialize StoreClass which properties can be access whole application.

This scenario how I achieve in asp.net MVC

Akil Vhora
  • 319
  • 1
  • 5
  • 21
  • 1
    Have you tried reading any books or websites on ASP.NET MVC? – Ivan Karajas May 01 '12 at 03:59
  • Create a static class and set the properties of the class in the app_start. More about app_start here http://stackoverflow.com/questions/2058621/difference-between-application-start-and-application-onstart – Paul May 01 '12 at 04:04
  • I have general knowledge about how MVC Pattern Work. – Akil Vhora May 01 '12 at 04:05
  • @AkilVhora - general knowledge of the MVC pattern will give you almost no help understanding how ASP.NET MVC works. Go to http://www.asp.net/mvc and start at the beginning. – Erik Funkenbusch May 01 '12 at 05:23

3 Answers3

1

You could use the global.asax events

http://www.techrepublic.com/article/working-with-the-aspnet-globalasax-file/5771721

Ivo
  • 8,172
  • 5
  • 27
  • 42
0

In ASP.Net project, you can place your configuration information in web.config. However, I prefer to place my config in an XML file and deploy it with the ASP.Net to web site. Here is one example to define my configuration class:

public class MyAppConfig {
  private static _config = null;
  // Configuration is a simple class with a list of properties
  public static Configuration Configuration {
     if (_config == null ) {
         _config = new Configuration();
         // parse XMl file and set properties
     }
     return _config;
  }
}

In your case, you can use MyAppConfig to get web application level configuration properties:

public class MyBaseClass : System.Web.UI.Page
{

  public override OnDo()
  {
      Configuration myConfig = MyAppConfig.Configuration;
      // use properties ....
  }
}

The advantage of placing configuration in your own XML file is that this component can be used in other apps such as console app with very little changes. However, you cannot write changes to the XML file in Web applications. Normally, I place writable information in databases to resolve the issue.

David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
0

For an event to be triggered before every action, you can do this.

Define a base controller and use it for all your controllers.

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
    }
}

Your controllers will look like this:

public class MyController : BaseController

OnActionExecuting will get fired before each action

For a session level or application level event, you should use global.asax

fozylet
  • 1,279
  • 1
  • 13
  • 25