Application_Start not firing in ASP.NET MVC app - asp.net-mvc

I've got an ASP.NET MVC app where the Application_Start event appears to not be firing. The symptoms are that an NLog log statement in that handler does not generate a log entry, and none of my routes get populated (so all my requests for controller actions return a 404).
Static files on the server (eg, favicon.ico) are served correctly.
I have log statements in Application_BeginRequest and Application_EndRequest. Those do generate log entries, both for the controller methods and the static files, so I feel pretty confident the app pool is configured correctly.
The problem shows up on our staging server, but not my local machine or our dev server.
Any idea what would cause this?

It ended up being a combination of things. The root cause was inconsistent versions of DLL's, and I solved that by configuring the project to copy the problem assemblies to my bin folder. On top of that, there was some code in Application_Error that was preventing the actual exception from being shown.

Related

IIS caching "The layout page could not be found at the following path" error

The Problem
I have a website running in IIS. If I rename or delete one of the layout page .cshtml files under /Views/ the site immediately begins throwing following yellow screen error as expected
The layout page "_Layout.cshtml" could not be found at the following path: "~/Views/_Layout.cshtml".
What surprises me is that if I recreate or rename the file so it is exactly like it was before, the yellow screen persists. Why is this particular 500 error sticky?
I currently think that this has something to do with IIS and is specifically related to error handling. The site immediately detects that the layout page file is missing. It does not immediately realize when the file is back in place.
Thanks!
Some interesting clues
This happens on all of my sites I've tried this on so far. It isn't related to a specific site
I tried this on two websites at the same time. On one site I repeatedly and consistently refreshed the page hoping for a success. On the other, I left it alone for several minutes before checking again. The site I leave alone will resolve its problems and find the layout page on disk again. The site I continually make requests to appears to display the error indefinitely.
What I've tried
I have reproduced the problem on Umbraco websites using Umbraco's default routing as well as regular MVC pages using custom routing. The problem is the same for both.
I don't have output caching configured in IIS
When I am reproducing the yellow screen error, I am able to reproduce the error in multiple browsers, so I don't believe it is related to browser caching
I checked on the httpRuntime in the root web.config and the fcnMode is set to fcnMode="Single"
I've fiddled around with the web.config customErrors and httpErrors. Nothing I've done here has affected the problem.
I am able to reproduce the problem on websites where there is no custom code for caching. No CDN. No load balancer.
Versions
IIS: I have reproduced the problem on Windows Server 2012R2 running IIS 8 and Windows 11 running IIS 10
CMS: All of the websites I have tested on so far are Umbraco 7 sites. However, I have reproduced the problem on pages that are routed using Umbraco's out of the box routing as well as pages that are just set up using MVC and aren't leveraging Umbraco.
It appears to be part of the behavior of FcnMode="Single". See https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.fcnmode?view=netframework-4.8. It isn't an issue with caching. It is a problem with the way that the site's file change notifications (FCN) are configured.
The sticky 500 behavior on renaming files happens when I use FcnMode="Single" but not when I use FcnMode="Default".
FcnMode="Single" will result in only a single object to monitor file changes. This single object is responsible for monitoring changes to files in the main directory and sub directories.
FcnMode="Default" will result in a separate object to monitor file changes for each directory.
Umbraco sites, by default, use FcnMode="Single". This makes sense because Umbraco sites cache under very deeply nested directories in /App_Data/. This can result in so many of these monitors that it can affect the performance of the site. There is a great explanation of FcnMode and why it matters for Umbraco here: https://shazwazza.com/post/all-about-aspnet-file-change-notification-fcn/
Unfortunately, it appears that the single file monitor can miss renames of files in some cases.

Where is defined MVC 4 WebApi custom errors when deployed?

Here is the thing, when I deploy the SAME PACKAGE (copy and paste) with the same Web.config to my local IIS and to the production server IIS i get two different responses when occur errors in the application.
in my local IIS:
{
"Message":"An error has occurred.",
"ExceptionMessage":"The method or operation is not implemented.",
"ExceptionType":"System.NotImplementedException",
"StackTrace":" at AppCenter.Web.Controllers.ApplicantsController.d__a.MoveNext() in e:\\Sample.Web\\Controllers\\HomeController.cs:line 86"
}
in the prod server IIS:
{"Message":"An error has occurred."}
I would like to know where is the configuration that makes it happen, for example, i would like the server to show the stack trace. The Web.config (where I though would be the place for that) is the same in both, so I think that it is somewhere else.
How can I do that?
The property IncludeErrorDetailPolicy on HttpConfiguration controls this information. As you can imagine, the behavior which you are noticing is deliberate so as to not expose too much information which probably could cause a security issue. If you would want a the config file to drive this setting, you could read it yourself and set this policy when webapi configuration is being setup.

What is the difference in ASP.NET startup between a recycle and a recompile?

When my MVC app is first deployed it works fine, up until the point when the application pool is recycled. After that, none of the routes work and I receive 404s for everything. Forcing a rebuild by editing the web.config or redeploying will bring the site back online, until the next recycle.
My MVC setup is a bit atypical. I'm doing quite a lot on startup, such as scanning assemblies for routes that need to be loaded. I feel like there must be some Global.asax event that only fires on the initial build but is not run on a recycle.
What are the differences between the recycle and the rebuild in terms of startup events?
Thanks,
Brian
The answer is apparently nothing. My problems stemmed from a garbage collection issue where resources weren't being cleaned up.

MVC: "The incoming request does not match any route." - with a default request

I am getting this error only SOMETIMES (apparently randomly, though I know there's really no such thing in computers).
I'm working on an MVC (release 1) site in VS 2008. The machine is running Windows 7 with IIS 7, but this instance is running under the Webserver built into VS (Cassini?). When I do an F5 to start running it, I will sometimes get an exception trap at the 4th line of the Page_Load method of Default.aspx.cs:
httpHandler.ProcessRequest(HttpContext.Current);
The exception method is:
The incoming request does not match any route.
Examining the HttpContext.Current.Request object shows a path of "/", and a rawUrl of "/Default.aspx?" - i.e, your normal empty request to a site.
The MapRoutes routine is pretty much standard except for a couple of IgnoreRoutes lines (ignore "pics/{*imgfile}", and ignore "{*favicon}"). The Application_Start routine has configuration for a StructureMap, and a call to SetControllerFactory before the call to RegisterRoutes, but it otherwise standard.
Usually, if I restart VS, the problem goes away. (And almost always if I restart the machine).
Any clues?
Thanks.
Set a default Page. F5 in asp.net will launch to whatever page is selected. However since in MVC, your View may or may not have an associated controller function, it can be trying to call an action that doesn't exist. Open up your project properties and set the debug start page to your site home controller action.
Sorry my poor english ...
I had this problem too! Misteriously the method Application_Start that call the routes disappeared in the Global.asax!!
vb.net:
Sub Application_Start()
RegisterRoutes(RouteTable.Routes)
End Sub
put this method in global.asax if it doesn't exist

View not found exception when runnig an ASP.NET MVC site from a network share

I'm getting a View not found exception when my ASP.NET MVC site is configured with is home directory pointing to a network share in IIS.
The weird thing is that this error appears intermittently, sometimes the view loads fine and others the same view fails.
I've tested this in IIS 6 and 7, both have the same problem.
I also have other ASP.NET WebForms sites on the same web server configured in the same fashion (files from a network share) and they have no problems, which makes me I think this is a bug in the MVC framework, specifically in the view look up process.
Have anyone else experienced something similar and/or know a solution?
Thanks,
Rafael.
The connection to shared drives is not persistent, meaning that the connections are "refreshed" in a sense when accessed. Since web forms compiles the entire site at run time the pages are accessed once during the compilation process (in a pre-compiled app you down even actually need the aspx pages).
However in MVC only the code in the controllers is compiled, leaving the views to be accessed by request at run time. If the view is not immediately available the MVC engine will throw an exception.
So if you hit MVC app the first time it will throw an error saying it can't find the view, but on subsequent requests immediately after it should find the view without issue. Until the connection state to the shared folder is closed, when you will get the error again.

Resources