I'm going to try my best to explain my situation here.
In my work, we had our whole website using basic ASP.NET, and we kind of created our whole own custom MVC framework from scratch, by creating our own HttpModules
, and HttpHandlers
.
This was because by that time, MVC did not even exist (at least here), we're talking about 12 years ago approximately.
We recently tried to move on, and try to upgrade our current project for a site to the new ASP.NET MVC 5 technology. We successfully managed to make it work (apparently), and at the same time keep our legacy business logic untouched.
Our business requirements led us to make a route for every possible request in our application, and that includes static files requests.
We included a catch-all route for our static requests. This route would delegate the request to our custom StaticResourceHttpHandler
, and the ProcessRequest
method is executed:
public void ProcessRequest(HttpContext context) {
string file = routeData.Values["file"].ToString();
string basePath = GetBasePath();
string fullPath = Path.Combine(basePath, file);
var extension = file.Split(new string[] { "." }, StringSplitOptions.None).Last();
context.Response.Clear();
if(File.Exists(fullPath) {
context.Response.ContentType = GetContentType(extension.ToLower());
using (FileStream stream = File.OpenRead(fullPath))
stream.CopyTo(context.Response.OutputStream);
} else
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
As you can see, our ProcessRequest
just check if the file exists in our file system, and if it exists it returns the file's content along with the correct Content-Type
header, and HTTP Status Code of 200 OK
.
This worked well for us so far, until now.
We are now o n the process of deploying our web application. We configured on the IIS that when a 404 is encountered, a redirect to an error page is made.
Our error page is a static aspx
file.
In our aspx
, we have the following lines at the beggining:
<%
Response.Status = "404 Not Found"
Response.StatusCode = 404
%>
With this, we make sure that a 404 Not Found
status code is sent along with the error page, and not a 200 OK
.
THe problem is that our custom static HTTP handler, does not execute this aspx
. In fact, it just outputs the content of it, with a status code of 200 OK
, as it was told to do in the code.
How can I solve this? How can I make the code in the aspx
file to be executed? Can it be executed programatically? If so, how can I do this?
What can be done?
preface: I have no experience with MVC, but I have experience with asp.net.
The problem is that a redirect through IIS is not the same as a redirect through the web.config in your application. Basically, a redirect through IIS changes the type of request to a normal request instead of a 404 request, effectively swallowing the 404 error.
How can I properly handle 404 in ASP.NET MVC? explains how to handle 404 errors in MVC in more detail than I can.
Short explanation: Depending on how you want to use this, choose 1:
As I have no experience with MVC, I cannot in good conscience recommend one of these 4 methods as the best or the worst method. I suggest you read the answers (in the above link) and see which one works best for you.
Also, I don't understand why you're going for MVC, but not go all the way to include your error page as a view as well.