Is there a way to tell ASP.NET app where to look for a static main html - asp.net

I have an ASP.NET WebAPI project and I have an AngularJS front-end in the same project. Right now, I have Index.html in the root of the project, and it works fine: when I enter something like localhost:8080, I can see the contents of my Index.html. However, I'd like to move it to an other folder, out of the root folder. Is there a way to config the application to look for a static startup Html file in a specific location? I'd like to avoid using MVC controllers just to point to the right file (I'd like to keep them out of my app).
I don't have any configuration for MVC in my Global.asax (that is no MVC RouteConfig) because I don't want to use MVC in my application because that's to be taken care of by Angular. Angular gets loaded from my Index.html. I just want to move my Index.html to Angular folder to keep it all in one place.
I am pretty sure there was a way to configure that stuff in web.config in the WebForms days, but I don't remember anything about that.

I found the answer to my question. Web.config needs this:
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear/>
<add value="SomeDir/SomeHtml.html"/>
</files>
</defaultDocument>
...

Related

How do I reload asp.net configuration file cache?

I have an asp.net application that sets the configSource attribute on the rewriteRules element in web.config to point to a separate config file:
<rewrite>
<rules configSource="App_Data\Config\RewriteRules.config" />
</rewrite>
My web app makes edits to the RewriteRules.config file programmatically, but my web app does not pick up the configuration changes after the file is edited and saved.
I have tried calling HttpRuntime.UnloadAppDomain() after editing the file. This successfully restarts my app domain, but the changes in RewriteRules.config are still not picked up. I have tried adding RestartOnExternalChanges="true" to the rewrite element, but this is apparently not supported on the IIS rewrite module. I have also tried ConfigurationManager.RefreshSection("rewrite/rules") but this does not seem to have any effect. The only way I can get the changes to take effect is to edit and save the main web.config file, but I am trying to avoid doing this programmatically for security reasons.
I am confused as to why HttpRuntime.UnloadAppDomain() does not cause external config files to be re-read. Is this expected behavior? Does the config file cache somehow exist outside the bounds of the app domain? Is there any practical way to achieve what I am looking to do?
Dude, the problem with your case is, related configSection definition is not marked as restartOnExternalChanges="true" in definition. For example; we created a custom config section for storing application urls in an external file and we create a section definition in web.config file like
<section name="pageUrlFormats" type="Kahia.Web.Configuration.PageUrlFormats.PageUrlFormatsSection, Kahia.Web" restartOnExternalChanges="true" requirePermission="false" />
so that asp.net knows if any change occurs in related file:
<pageUrlFormats configSource="Config\PageUrlFormats.config" />
application domain restarts. This goes same for all config section definitions, including UrlRewrite module's definition.
What you have to do is, find definition of related module. In this scenario, it is at apphost.config at C:\Windows\system32\inetsrv\config\applicationHost.config
In that file, look for rule section definition, it starts like
<section name="rules"
You have to add restartOnExternalChanges="true" attribute to that config file.
IIS7 configuration system uses the same syntax as the .Net framework configuration system, but is a different implementation that has some behavior differences. The restartOnExternalChanges thing is a feature of the .Net framework configuration system that is not supported by the IIS7 configuration system. The url rewriter module uses the IIS7 configuration system.

Can WebAPI serve static image files for only certain routes?

I have a ASP.Net MVC 4.5 WebAPI controller (IIS8) that can serve static images files from a repository. The URLs I want to use are like;
http://example.com/blob/image/123412341324.jpg
The trouble is '.jpg' here appears to be going through the static file handler, which for the most part is what I want to keep. I don't want all static files to go through ASP.Net just the ones on this URL path.
Is there any way I can configure ASP.Net to serve up these files without runAllManagedModulesForAllRequests and without modifying the url such as removing the file extension? Ideally, an IIS routing mechanism that looked at the folder path /blob/image/;
http://example.com/blob/image/123412341324.jpg -- route through ASP.Net WebAPI
http://example.com/staticimages/123412341324.jpg -- do not route through ASP.Net
http://example.com/anywherelese/123412341324.jpg -- do not route through ASP.Net
I know I could write a separate 'IHttpHandler' but I'd like to route it through the MVC WebAPI controller if possible.
So it turns out this is quite easy to do with handler mappings. I tried a few wildcards but although *.jpeg works blob/image/*.jpeg does not. Eventually, with a little help from this post I came up with the following entry in my app's web.config which does what I need and routes all requests for this controller;
<system.webServer>
<handlers>
...
<add name="my-blob-jpeg-ExtensionlessUrlHandler-Integrated-4.0" path="blob/image" verb="GET,HEAD,POST,DEBUG,DELETE,PUT,PATCH" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
...
</handlers>
</system.webServer>
It's not specific to image files but I don't plan on putting any other static content along that path so I'm good.

Migrate ASP from root to subdirectory

I'm in the process of converting an existing ASP website into PHP while retaining the ASP website as an older version. Since I'm not an ASP developer, I thought it would be simple as moving the contents of the root directory into its own directory that I've labeled as v1.0 so that the ASP version can be viewed by going to www.mysite.com/v1.0 while the new PHP version (v2.0) can be viewed by going to www.mysite.com
Herein lies the problem. Doing this causes flags all kinds of errors (all related to "Server Error in '/' Application. Runtime errors"). The best I can figure out is that the web.config file needs to be tweaked in the v2.0 directory. Can any of you ASP experts recommend a simple solution to make this happen? I would like to have an empty root directory if possible since the new v2.0 version will be in all PHP and want to "self contain" the ASP version within the v1.0 directory as much as possible.
Thanks!
After a bit of wrangling, finally figured out that I needed to set up v1.0 as its own application under the IIS control panel. Once that was done, then all the contents of the root directory could be moved over to the v1.0 sub-directory and viewed via www.mysite.com/v1.0 -- thanks to Sean! for his help!
Most Classic ASP pages use Server Side Includes, if the site is using a SSI such as:
<!-- #include virtual = "include-file.inc" -->
, then you will need to change all of these to be
<!-- #include virtual = "v1.0/include-file.inc" -->.
I assume that you are using IIS 7 or IIS 7.5, since you are not seeing the actual error, you will need to modify your web.config file.
<configuration>
<system.webServer>
<asp scriptErrorSentToBrowser="true" />
<httpErrors errorMode="Detailed">
</system.webServer>
</configuration>
Reference:
http://learn.iis.net/page.aspx/564/classic-asp-script-error-messages-no-longer-shown-in-web-browser-by-default/
Update
Since it now sounds like you are using ASP.NET, you would need to change your web.config as below to see the full error:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>

ASP.NET 3.5 application with multiple web.config files (IIS 7)

We are working on a web application that creates more web applications.
Each web application will have to get a Url Rewrite rule (URL REWRITE MODULE 2.0).
As far as I know, there's no way to add such rules without modifying the web.config file (am I right??).
So my plan was to work with multiple web.config partial files. One main .config file, and lots of .config files per application (every file will contain it's web application url rewrite rules).
This way sounds a little bit messy, but I can't think of anything else, and suggestions will be welcomed.
So is it possible to use very-multiple web.config files for the root application?
Thanks in advance, Gal.
This following Tag will do the trick.
The absence of this tag was the main reason for my problem when i using with two web.config files for my two different application running in my website.
**<location path="." inheritInChildApplications="false">**
<system.web>
<!-- ... -->
</system.web>
**</location>**
Every application must have a full web.config and not partial, exept if you go with net 4
The trick is to use a lot the remove command on the other inside web.config and remove the parents setting that must not used on this.
For example if on the main root you have the a module that you do not won to use it on the other trees, you use the remove command on all other web.config to remove it. Especial the modules that are on one Bin and not on an other directory bin.
<httpModules>
<remove name="TheHttoModuleNotNeedHere" />
<remove name="AnonymousIdentification" />
... add here your other modules for that directory...
</httpModules>
The remove command is working for almost all sessions on config.
You can do make it work, I have done it, but its a lot of work to find all the conflicts/unnecessary configs and remove it.
For some other session there also the clear command. For example on role Manager you can clear all and add new.
<roleManager enabled="true" ...>
<providers>
<clear />
<add name="MyName" ... type="System.Web.Security.SqlRoleProvider" />
</providers>
Hope this help as tips to make it work.

IIS7: disabling HttpModule in subapplication - sites, application and virtual directories

I have a few aspx files in a "Cache" folder in my application and I do not want HttpModules to run for those files in that folder. I tried having a web.config in subdirectory but learned that HttpModules take the root web.config and not that of the subdirectory.
Reference 1, Reference2. So I decided to have this directory as a sub application as per suggestion here and here.
So I configure my application,then "add application" , map it to this directory, which already was inside this application and boom, it fails. It works for a static html file, but aspx files are not available.
My question is, how do I configure a sub-application in IIS7 so that the sub-application can have its own web.config and there I can disable HTTPModules of the root application
Edit:In fact I tried creating a subapplication within my main application and it did not work. Can some one point me to any article on how to configure a sub-application in IIS7 ?
Edit2: adding the error image. So how should I configure the child app pool. The child app runs in the same app pool as that of parent
Edit3: sorry, the child was running on a different app pool. A generic app worked(without modules). I am marking the answer after I try out the modules.Thanks for your help guys. There is something specific in my parent app web.config, which I am going to hunt down now.
EDIT: Actually both the answers provided below are correct. If you are using IIS7 integrated mode your modules should be in system.webServer and if IIS7 - classic mode your modules (and handlers?) should be in system.web
JKG has the right answer for IIS6, but the syntax is a little different in IIS7:
<system.webServer>
<modules>
<remove name="MyModule"/>
</modules>
</system.webServer>
The web.config will always inherit from its parent if it's in the same web application but you can clear the entire thing or remove an item like so:
From the child web.config (clear all or remove an item)
<httpModules>
<clear />
<remove name="MyModule"/>
</httpModules>
From the parent config by using the location tag...
<location inheritInChildApplications="false">
<system.web>
<!-- ... -->
</system.web>
</location>
http://www.jaylee.org/post/2008/03/Prevent-ASPNET-webconfig-inheritance-and-inheritInChildApplications-attribute.aspx

Resources