ASP.NET MVC - Show 404 general views without a controller

I look at this question where the responder shows a great way to throw a NotFoundException from the Application_Error method in Global.asax.

It's great, but I would like to change it a little ... just not sure how to do it.

I have a NotFound view and an Error view in the Shared views folder. I would like to continue this and not have an ErrorController.

Is there a way to throw a 404 NotFound error when the “controller” does not exist, but should not have an error controller?

IE: if you visit http://example.com/asdf , in which asdf is an invalid controller, I want the "NotFound" view to be loaded from my View Directory.

Edit

Basically, the problem I am facing is "DRY." I could create an ErrorController, however, if I do, then I should have the same view in two places. One in my Views / Error folder is only for this situation and one in my Views / General folder, with which I can download Error or 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

If the EventController is explicitly for events, but tried to load NotFound in the view (hoping it would pull it out of the shared directory ... but I had no luck.

+3
source share
3 answers

, , , ( , ).

0

hack/work ,

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

Error/Index, , ,

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

, ... ... .

0

Source: https://habr.com/ru/post/1775313/


All Articles