ASP.NET MVC - 显示 404 共享视图,无需控制器

发布于 2024-10-02 19:21:43 字数 1590 浏览 0 评论 0原文

我正在看 这个问题 其中回答者展示了一种从 Global.asax 中的 Application_Error 方法抛出 NotFoundException 的好方法。

这很棒,但我想稍微修改一下......只是不知道如何修改。

我的共享视图文件夹中有一个 NotFound 视图和一个 Error 视图。我想继续这样做,并且不必有 ErrorController。

当“控制器”不存在时,有没有办法抛出 404 NotFound 错误,同时仍然不必有错误控制器?

IE:如果您访问 http://example.com/asdf 其中 asdf 是无效控制器,我想要从我的共享视图目录加载“NotFound”视图。

编辑

基本上我遇到的问题是“DRY”。我可以构建一个 ErrorController,但是如果我这样做,那么我必须在两个位置拥有相同的视图。一个在我的 Views/Error 文件夹中,用于这种情况,另一个在我的 Views/Shared 文件夹中,我可以从任何控制器加载错误或未找到视图。

我也尝试过,

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

其中 EventsController 显然用于事件,但尝试在 NotFound 视图中加载(希望它能从共享目录中提取......但我没有任何运气。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧 2024-10-09 19:21:43

这个问题的答案可能就是您正在寻找的,除非您可能无法将页面放入共享文件夹中(为此,您可能需要控制器)。

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

卸妝后依然美 2024-10-09 19:21:43

我发现了一些解决我的问题的黑客/解决方案,尽管它实际上并没有消除对控制器或视图的需要

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

基本上在这里我将用户重定向到错误/索引页面,但是当他们到达那里时,这就是控制器所做的。

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

它会抛出适当的错误并将它们带到适当的页面...但它并不能消除对控制器或视图的需要...尽管索引视图完全是空白的。

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.

梦幻之岛 2024-10-09 19:21:43

创建一个 VirtualPathProvider。 ASP.Net 使用它来查找文件(视图)。

示例代码: http://padcom13.blogspot.com/2009/04/virtualpathprovider-示例.html

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文