1

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

Community
  • 1
  • 1
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • 1
    I don't know how you implement the custom controller factory for it, but I want it when someone tells you how :P – CatDadCode Nov 18 '10 at 23:18
  • possible duplicate of [ASP.NET MVC - Show 404 Shared View WITHOUT a Controller](http://stackoverflow.com/questions/4199951/asp-net-mvc-show-404-shared-view-without-a-controller) – Daniel A. White Nov 18 '10 at 23:32
  • 1
    Really @Daniel White? That's the question I LINKED TO since it was a question that I ASKED. Did you read **this** question, since I explain why I asked it the way I did? – Chase Florell Nov 19 '10 at 00:27

2 Answers2

1

I don't get the point

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:

  • is more time consuming to develop,
  • is more prone to errors,
  • is more complicated,
  • affects a larger surface of an application in case of having bugs?

Agile developers are supposed to be VERY LAZY when it comes to overengineering.

David Murdoch
  • 87,823
  • 39
  • 148
  • 191
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • so you're saying that the grain of MVC is to have a pointless controller and a pointless view folder and a pointless view all just to serve a static content view? Not only that but I also have to have "duplicate" views (which are identical) because of this pointless method that I'm currently using? Instead of my idea which would simply build a `CustomControllerFactory` and reuse the existing view in the shared folder. This just doesn't make sense to me. – Chase Florell Nov 19 '10 at 05:43
  • Why are you having duplicate views? If you have a controller you can always direct to the same view. Why having duplicates at all? – Robert Koritnik Nov 19 '10 at 14:31
  • because if the view doesn't exist, MVC throws a 500 error. So I have to have a "blank" view in the Errors folder just to satisfy the requirement. Hence why I'm trying to override the default ControllerFactory. – Chase Florell Nov 20 '10 at 20:49
0

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.

Community
  • 1
  • 1
Martin Peck
  • 11,440
  • 2
  • 42
  • 69
  • not really. This still requires a `Controller` for the errors. I'm trying to avoid that all together. It might be "more work" to do it the way I want, but I "think" the result should be cleaner in the end. – Chase Florell Nov 19 '10 at 00:29
  • that's just it. My `/shared/notfound.vbhtml` file IS a view, and I'm already using it in a lot of code where I'm throwing a `ResourceNotFoundError`. I don't want to have to copy/paste the same file into an html file just to serve an identical error as everywhere else. Unfortunately the ResourceNotFoundError doesn't work from the Application_Error method in the Global.asax because that error happens before MVC touches the request. – Chase Florell Nov 19 '10 at 05:45