Complex Configuration Class asp.net - c#

I have a complex class for configuration, which holds all the configuration data, and I'm reading this class in a separate class that exists in the business class library. In my web project, I am reading this in a pagebase class - which all my aspx pages inherit. I have the class as a property in pagebase, and I can access it in my aspx pages fine. But how do I access this in the usercontrols? Is there a global way to keep this class in memory (I can't have it as static, because its different per user) and I'd rather not use sessions. I was thinking there is a way to have it as part of a global properties somewhere, so I can always use it anywhere in the web project.

If its on all your pages you can just cast the page object to your base page and access the property.
inside a usercontrol.
((MyBasePage)this.Page).MyConfigObject

You can put it in HttpContext.Current.Items dictionary around page creation time. It will be avaialbe during lifetime of that request.
Note: Tey to find other ways to achieve whatever you want to achieve without global/per-request state...

Related

Injecting Partial View Dynamically

I am developing UI for a dynamic platform, a user can add his/her own properties to classes in platform. I will go through an example to make my question clear:
Say we have a class X with ID and DisplayName properties. These are default initial properties of class X. In the ASP.Net web Application I have created an associated form for class X with both properties bound to model. Yet users can add fields to this class and I want to create associated sections in the UI, but with condition that the user can offer correct HTML (or aspx) markup for new field(s) and the markup would render in the UI. This is to avoid changing code for little domain manipulations and does not require code recompile and server restart.
Said Otherwise, I want a functionality like saving partial views in the DB and loading them in the UI.
I have found this Question so close to mine, yet the provided answer is not good enough.
If you can point me to any project, tutorial or sample code, I will appreciate it.
What I have done so far: I have checked Naked Objects for MVC, yet I think it is an overkill for my question. MVC Form Factory is very interesting but I need those added variables be accessible using provided template.

What .net class to inherit from to use asp.net sessions, but isn't a page

I have a asp.net webapplication in vb.net. Sessions are available on a page that inherits from Inherits System.Web.UI.Page. To have better separation of concerns I'm thinking about creating a class that knows about server sessions, but isn't a page. I could instance the class with a session-object or set a property, but i was wondering if is it possible to create a class that knows about the session. What should the class inherit from?
What would be a different design pattern?
You can just access your session with:
HttpContext.Current.Session
Any class you create as base class can have a property with returns the HttpContext.Session, it is not bound to your Page object.
HttpContext MSDN

Get Page instance externally in WinRT

I am currently developing a Windows 8 App which contains 2 pages(page1 , page2).
After navigating form page1 to page2,
using this.Frame.Navigate(typeof(AnotherPage));
how can i access the instance of page2 form page1,(We can implement it by having a static instance property and set it when the constructor is called )
But is there any in-build feature to access the current instance of the page from outside of the current page, Or is this pattern of programming is recommended in WinRT paradigms, is there any way to implement my scenario.
First thing is - by default the instance of the previous page is not kept in memory after you navigate to the next page by default - not unless you change the default NavigationCacheMode of the page, which might not be a good idea unless the page takes a lot of time to load and is like a hub page that you go back to a lot.
The approach I would suggest is to use the MVVM pattern, so pages never have to talk to each other and it's the view models that would do it. Then use a pub/sub pattern with a helper like the Messenger class in MVVM Light Toolkit to send weak events/messages instead of adding tight coupling between objects.
All in all - it depends on what you want to do and why you want your pages to talk to each other. You could pass a parameter in the Navigate() call (make sure it's just a primitive type though if you want to support app suspensions using the SuspensionManager class and built-in serialization from the Frame class). You could also use some sort of a global repository or settings service to share data between pages.
If you're not willing to invest in these things - using the plain old static class might be enough for you. Whatever works.

How to inherit a page class from a class library

Ayup
I might be asking a dumb question, but I have a client for whom I need to build many websites (10+) (asp.net 3.5) which will all the pages on each site will have the same codebehind, but the sites will launching in different regions and whilst following the same template, will have different content.
I have built and launched site 1, and sites 2, 3 & 4 is nearly live, but it occurs to me that as all the sites are basically the same, the code is going to get more complicated to update as it will be duplicated, so if I need to do a bug fix on one site, I'll need to do the fix on all websites (and this is going to get complicated.)
I was wondering if it possible to somehow create a class library of all the current aspx.cs files, reference this dll in each website and then inherit these classes into the .aspx.cs files. So default.aspx in each site would still have a CodeFile of "Default.aspx.cs", but Default.aspx.cs would inherit the corresponing class from the dll:
using WebPagesClass;
public partial class _Default : WebPagesClass._Default
{ }
The reason for doing it like this is that if I need to change any code on a specific website (for minor changes in languages for instance), I can override the page functions and change the parts required. For all other pages which have not cha, I can just copy from a single website.
Is this vaguely possible? If not anyone one got any killer suggestions of how to manage so many websites from a single codebase?
Cheers
T
Sure you can do this. But you will need to ensure that your base class inherits from System.Web.Page.
So create a library, add the necessary references (System.Web, etc.) and create your base class:
public class MyBasePage: System.Web.Page
{
}
Then you can specify that as your base class for all your other pages.
As for using this single class as the direct code-behind class for all your aspx pages, this may get tricky because the pages will likely have their own individual asp.net controls on them, and therefore the code-behind classes for each page will indeed be slightly different.
I assume you are wanting to do this work because there is a bunch of similar code in the pages, but I imagine there is also some specific code/control declarations as well. So I would create the base class and put the common code in there, and just inherit from that class...
It is possible to move all differences between the sites into ASP.Net Themes or config files? Sounds like a better approach to me

How and where are variables that are used throughout the ASP.net Project created/stored?

I want to be able to store variables that are accessible to all of the other files in my ASP.net project but can be modified programatically. What I am looking for is something akin to being able to pull information from the web.config file for say the database connection strings (ex System.Configuration.ConfigurationManager.ConnectionStrings["cnDatabase"].ToString())
I have variables that have values that are shared amongst all of the other pages and I would like to be able to modify in one location instead of having to update the same value in 4+ aspx.cs pages.
AppSettings in the Web.Config file
or
public/internal constants
If you want something that is global to the application where you can add custom variables, use a Global.asax. Anything you add here will be available through the Global variable inherited in all pages and controls. If all you need is a key/value store, you can use the Application or Session static variables that are inherited in all pages and controls. Application (which is just a static instance of HttpApplicationState) is an object that you can use as a Hashtable to store custom values that will be available on all pages for all users. And, Session (HttpSessionState) is available the same way for use as a Hashtable, but the values you store will be unique per user session.
Note: if you need to access any of these objects outside of a page or control (ie. a custom class used within the context of a page request), you can get a reference to them through the current http context (HttpContext.Current).
I think what you're looking for is the Settings feature of a project.
Here's a good introduction: http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
Note: this isn't available for "Web Site" type projects, just ASP.Net Web Application projects.

Categories