IIS 7 Default document setting for a mapped handler url - c#

I have a web site that serves only one page from the root. The page (call it stuff.htm) is generated by a custom handler, and doesn't physically live on disk.
I'd like stuff.htm to be the default doc for the site, but the standard configuration method of making it the default doc fails since the regular ASP.Net DirectoryListingModule is trying to do a Server.Execute of the file. (At least, that's what I can infer is happening.)
I know I can write code through a default.aspx that does what I need, but I want to avoid writing code for this. Any known configuration tricks to supersede the default handler behavior without having to override the default handling behavior?

Is ASP.NET set to handle the file extension you are trying to serve? If so I would expect it to work correctly; otherwise here are some "cheap and dirty" answers.
The "cheap and dirty" answer is to create an empty file named stuff.htm at the root, set it as the default page, and then ensure that ASP.NET is mapped to handle the .htm file extension (or whatever you are actually using).
The second "cheap and dirty" answer is to create a default.htm page and then set it up as a redirect to stuff.htm.

Related

Is there a non-default alternative for Properties.Settings.Default?

In ASP.NET, when you want to reference application settings, you use the Properties.Settings object. Immediately after that, there is a Default object which contains all the application settings.
My question is: Is there a way to have something other than that Default profile in Properties.Settings.Default? Something like a custom non-default area for settings. I have a web application that has lots of modules and each one has a bunch of settings relevant only to that module. I'm looking for a way to keep them separated.
The closest thing I've found is this SO question (Using Properties.Settings.Default and Setting Alternate Profiles?), but it wasn't really the answer I expected and I wanted to give it another shot and see if anyone had discovered something new.
Edit: This MSDN document explains EXACTLY what I want (http://msdn.microsoft.com/en-us/library/bb397748%28v=vs.110%29.aspx). However it's just not working for me. The supposed Settings File type doesn't exist.
Could you create extra web.config files and use the WebConfigurationManager to read from the appropriate config file?
Edit: Sorry, I had originally said CongurationManager you will want WebConfigurationManager.
Assuming you meant "I want to create my own section" (of settings)
see How To create Custom Config Sections.
if you choose, you can it further and create separate MyCustomSettings.config files , e.g. <pages configSource="pages.config"/> see configSource

How to change a website default document programmatically on the fly

Using ASP.NET 4.0, IIS 7.5.
I have a website engine, I have just implemented a way for this to tell if it's being loaded on mobile and instead of loading Controls\MyControl.ascx it loads Mobile\Controls\MyControl.ascx. This works well for my controls and also my MasterPage.Master file.
What I can't figure out however is how I can do the same with Default.aspx. This needs to be done on the fly programatically as I need to be able to check if it's mobile version. I was thinking of doing something on a pre-init event in globals but not sure if that's the best way.
Note: I don't want to use inline code on Default.aspx and just display different content base on my Mobile flag as my scenario goes one step further by basing the file on customer as well and this would mean having one huge Default.aspx for all customers which wouldn't be manageable.
Changing the default document on the fly is not possible in any practical sense.
Writing to the web.config on the fly to load a mobile version of a default page is quite frankly terrible and not an answer to the true context of your issue. I would feel irresponsible as a developer if I even proposed this as an answer to loading a mobile version of a default page.
I was trying to help you solve your problem and not just answer the base question in the title. As we all know, changing the web.config will restart your application and would not serve as a true solution, as you could not do this and achieve any kind of performance.
Here is the BEST alternative (IMHO) to dealing with mobile browsers.
http://51degrees.codeplex.com/
HTH!
For anyone else looking at this I have found a solution but I am not sure I will implement it as I don't like the idea of updating the web.config file at run time. Using the Microsoft.Web.Administration namespace you can update the server.webServer -> defaultDocument section programmatically. Doing this allows you to change the path to default.aspx and it will load based on the variables you set.
This link should provide more information: http://blogs.msdn.com/b/saurabh_singh/archive/2007/11/24/accessing-iis-7-0-features-programmatically-from-configuration-file-s-c.aspx
Also, the Microsoft.Web.Administration dll isn't available directly in VS so you need to add it from %windir%\syswow64\inetsrv (64bit version).

Custom folder for custom codes

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#?
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).
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.

Captcha control not working with url-routing (ASP.NET 4.0 WebForms)

ASP.NET 4.0 C# WebForms
I have route.Ignore("{resource}.axd/{*pathInfo}"); in my global.asax for my ajax to work correctly.
The control is a "MSCaptcha".
It shows the path in sourcefile http://localhost:666/Project/CaptchaImage.axd?guid=96f830ee-6fb9-42ad-9ff4-d6484ffdcbe4 but does not show the "image".
Can I add something to my global.asax to get the captcha control to work?
Any suggestions?
It looks like you want to be able to ignore your captchaImage.axd at different depths in your virtual folder hierarchy? This would require handling multiple segments via routing which isn't convenient in your case. It would be better to "fix" the location of the CaptchaImage.axd to a certain path, ignore that path via routing, and always reference the CaptchaImage.axd at that one location.
Details on asp.net routing.
http://msdn.microsoft.com/en-us/library/cc668201.aspx
You do need to set it to ignore at the different levels as mentioned by Kenneth. In my app, we only dealt with one level deep. However, you can use the following code and set multiple levels.
routes.Ignore("{parent}/{sub}/{resource}.axd");

Test for default document request?

Is there a way to programmatically know when someone is using the Default Document feature of IIS to access my page (that is, the name of my .ASPX isn't in their URL anywhere)?
The only way I can think of is check the URL (to see if it's requesting http://localhost/virtual/ instead of http://localhost/virtual/default.aspx) and consider that the default document...
Not a feature for MVC or if you have url rewriting... and I don't know if an IIS feature for this either, unless for some reason it was added as a module and available via IIS 7...
I don't think there's a way to do this. If the default document feature is used, IIS just puts the document name into the URL before passing it to ASP.NET.
What are you trying to accomplish with this? You can turn default documents off if you like.

Categories