3

I'm looking at this so question where the answerer shows a great way to throw a NotFoundException from the Application_Error method in the Global.asax.

This is great however I'd like to modify it a little... just not sure how.

I've got a NotFound view and a Error view in my Shared views folder. I'd like to continue with this and not have to have an ErrorController.

Is there any way to throw a 404 NotFound error when a "controller" doesn't exist, while still not having to have an Error Controller?

IE: if you visit http://example.com/asdf whereby asdf is an invalid controller, I want the "NotFound" view to be loaded from my Shared views directory.

Edit

Basically the issue I'm running into is that of "DRY". I could build an ErrorController, however if I do this, then I have to have the same view in two spots. One in my Views/Error folder for just this situation, and one in my Views/Shared folder whereby I can load up the Error or the NotFound View from any controller.

I also tried this

Private Sub BaseGlobal_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
    Dim exception As System.Exception = Server.GetLastError
    Response.Clear()

    Dim httpException As HttpException = TryCast(exception, HttpException)

    Dim routeData As RouteData = New RouteData()
    routeData.Values.Add("controller", "Events")

    If (Not httpException Is Nothing) And (httpException.GetHttpCode = 404) Then
        routeData.Values.Add("action", "NotFound")
        Server.ClearError()
        Dim errorController As IController = New UrbanNow.Core.Controllers.EventsController
        Response.StatusCode = 404
        errorController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
    End If
End Sub

Where the EventsController is obviously for events, but tried to load in the NotFound view (hoping it would pull from the shared directory... but I didn't have any luck.

Community
  • 1
  • 1
Chase Florell
  • 46,378
  • 57
  • 186
  • 376

3 Answers3

0

The answer to this so question might be what you are looking for except you may not be able to put the page in the shared folder (to do that you might need a controler).

Community
  • 1
  • 1
HitLikeAHammer
  • 2,679
  • 3
  • 37
  • 53
  • The "most upvoted" answer on that page says that you do it in the web.config. Problem with that is that it either requires a controller or a static page. Both of which I want to avoid. I'm trying to do it exclusively with my Shared controller. – Chase Florell Nov 17 '10 at 03:59
0

I found a bit of a hack/work around solution to my problem, though it doesn't actually remove the need for a controller or a view

Private Sub Application_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
    Dim exception As System.Exception = Server.GetLastError()
    Dim httpException As HttpException = DirectCast(exception, HttpException)

    Dim routeData As New RouteData()
    routeData.Values.Add("controller", "Error")

    If exception IsNot Nothing Then
        If httpException.GetHttpCode = 404 Then
            routeData.Values.Add("action", "Index")
            Server.ClearError()
            Dim errorController As IController = New MyApp.Core.Controllers.ErrorController
            Response.StatusCode = 404
            errorController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
        End If
    End If
End Sub

Basically here I'm redirecting the user to the Error/Index page, but when they get there, this is what the Controller does

Public Class ErrorController : Inherits MyApp.Core.Base.BaseController
    Function Index() As ActionResult
        Throw New ResourceNotFoundException
    End Function
End Class

This works, throws the appropriate error and takes them to the appropriate page... but it doesn't eliminate the need for a controller or a view... though the Index view is completely blank.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
0

Create a VirtualPathProvider. It's used by ASP.Net to find files (views).

Example code: http://padcom13.blogspot.com/2009/04/virtualpathprovider-example.html

jgauffin
  • 99,844
  • 45
  • 235
  • 372