Reputation: 47377
I wrote this question a few days ago, and though I got a few upvotes, I didn't get much action. This is why I have chosen to re-visit this question in a new way.
I'm trying to send a user to my NotFound
view which is located in my Shared directory whenever they request a controller or action that doesn't exist. You'll see my own answer in the previous question which is a working work-around, however it still requires me to have an ErrorController
and an /Error/NotFound.vbhtml
View. I think that this is rather ridiculous since I'm only serving up static content from that view.
What I'd like to do is render the view from the shared directory without the need for a Controller. This is where my idea of a Custom Controller Factory comes in... I just don't know how to go about doing it.
How can I build a Custom Controller Factory that will behave exactly the same as the Default Controller Factory, except for when I need to show the NotFound view (while keeping the source URI intact of course)?
Upvotes: 1
Views: 1077
Reputation: 105019
Isn't writing a custom controller factory actually more work than having an additional controller with one action that handles 404s?
And if you have some sort of a GeneralController
that handles application wide functionality (like settings or similar), this action can be part of it anyway.
So. Which one between controller factory and controller:
Agile developers are supposed to be VERY LAZY when it comes to overengineering.
Upvotes: 1
Reputation: 11544
This should help a lot...
How can I properly handle 404 in ASP.NET MVC?
... and I would recommend you go with this approach.
I think what you're trying to do is likely to be going against the grain of ASP.NET MVC, or at least introducing something that isn't going to be obvious to others looking at your code.
You can achieve something like what you want by using custom errors. In web.config you can do something like this..
<customErrors mode="On" >
<error statusCode="404" redirect="~/Views/Shared/NotFound.aspx" />
</customErrors>
... but because this aspx page lives in the Views folder you have to go into the web.config that lives in the Views folder and stop it from preventing direct access to the views.
If this page (NotFound.aspx) is written as an MVC view you're going to have trouble. You'll find a number of things (this.Html for example) may not be set up correctly.
Given that you're accessing this file as if it's classic ASP.NET you might want to write it as classic ASP.NET, and let it live outside of the Views folder.
Finally, you might want to use a different redirect mode...
<customErrors mode="On" redirectMode="ResponseRewrite" >
... so that your 404 url is that of the page requested.
Upvotes: 0