1

We're having a restructuring on our application and currently the idea is to break the codes into Core library codes + customized codes for our developer.

I'm thinking of the possibility to have a folder (i.e. 'custom') that is empty by default, and when the developer need to customize any codes either from existing asp pages or new pages, they just need to put them into the folder and it will work. Example:

Lets say core folder store the default asp pages.

core\customer\createCustomer.asp <-- the default page

And when the developer want to overwrite that page, he needs to copy that asp page to the custom folder, like

custom\customer\createCustomer.asp <-- modified asp page

The application will automatically load the one in the custom folder rather the one in the core folder.

Is this doable in C#?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ipohfly
  • 1,959
  • 6
  • 30
  • 57

2 Answers2

0

You can use a VirtualPathProvider to load a different file than the "actual requested one". This works well with IIS and caching for instance as well.

Basically you inherit the VirtualPathProvider and override the FileExists, GetFile, and DirectoryExists, GetDirectory methods (there's an example in the linked page). Then, in your AppInitialize, register the provider with

HostingEnvironment.RegisterVirtualPathProvider(sampleProvider);

By the way, don't forget to have a different (non-editable) page so the user can revert any changes that was made, in order to restore a potential misedit so to speak. I would probably have a simple version control system and use commit whenever the user made changes, and allow the user to revert to a previous changeset.

Patrick
  • 17,669
  • 6
  • 70
  • 85
  • Hmm i have problem finding where should i invoke the AppInitialize method. I found this link http://stackoverflow.com/questions/329936/when-appinitialize-method-get-invoked-in-asp-net but not sure it helps .. – ipohfly Oct 08 '12 at 10:44
  • The post you linked to describes the method. It's invoked when the application is started, and can only be defined once in an application. Read about the [Application life cycle](http://msdn.microsoft.com/en-us/library/bb470252(VS.100).aspx) and call the `RegisterVirtualPathProvider` once when your web site is created, such as in a suitable event in the global.asax file. – Patrick Oct 08 '12 at 15:47
0

This MSDN article explains how to use an IHttpModule implementation to intercept HTTP requests and perform custom actions (they point out logging, but since you're intercepting the request you might as well fetch some different content, such as your 'customized' code).

Alex
  • 23,004
  • 4
  • 39
  • 73
  • this looks promising, but by 'fetching code' do you mean redirect the request to the different content? – ipohfly Oct 08 '12 at 10:39
  • There are two ways I can think of: redirect the request to the customized content, or don't redirect but (behind the scenes) return the customized code and forge the response to include it instead of the requested one (of course, the site would need to always refer to "core" content and not be aware of it). – Alex Oct 08 '12 at 12:10