title: ASP.NET MVC - Show 404 Shared View WITHOUT a Controller
body: <p>I'm looking at <a href="https://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/620559#620559">this so question</a> where the answerer shows a great way to throw a NotFoundException from the Application_Error method in the Global.asax.</p>
  
  <p>This is great however I'd like to modify it a little... just not sure how.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>IE: if you visit <a href="http://example.com/asdf" rel="nofollow noreferrer">http://example.com/asdf</a> whereby asdf is an invalid controller, I want the "NotFound" view to be loaded from my Shared views directory.</p>
  
  <h3>Edit</h3>
  
  <p>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 <strong>same view</strong> 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.</p>
  
  <p>I also tried this</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>