現在、から継承するBaseControllerクラスがありSystem.Web.Mvc.Controller
ます。そのクラスには、HandleError
ユーザーを「500-おっと、めちゃくちゃにした」ページにリダイレクトする属性があります。これは現在、期待どおりに機能しています。
この作品
<HandleError()> _
Public Class BaseController : Inherits System.Web.Mvc.Controller
''# do stuff
End Class
また、404ページがActionResultごとに機能しており、これも期待どおりに機能しています。
この作品
Function Details(ByVal id As Integer) As ActionResult
Dim user As Domain.User = UserService.GetUserByID(id)
If Not user Is Nothing Then
Dim userviewmodel As Domain.UserViewModel = New Domain.UserViewModel(user)
Return View(userviewmodel)
Else
''# Because of RESTful URL's, some people will want to "hunt around"
''# for other users by entering numbers into the address. We need to
''# gracefully redirect them to a not found page if the user doesn't
''# exist.
Response.StatusCode = CInt(HttpStatusCode.NotFound)
Return View("NotFound")
End If
End Function
繰り返しますが、これはうまく機能します。ユーザーがhttp://example.com/user/999(userID 999が存在しない場合)のようなものを入力すると、適切な404ページが表示されますが、URLは変更されません(エラーにリダイレクトされません)。ページ)。
このアイデアを機能させることができません
ここで問題が発生します。ユーザーがhttp://example.com/asdf-と入力すると、一般的な404ページに移動します。私がやりたいのは、URLをそのままにして(つまり、他のページにリダイレクトしない)、単に「NotFound」ビューを表示しHttpStatusCode.NotFound
、をクライアントにプッシュすることです。
たとえば、https://stackoverflow.com/asdfにアクセスすると、カスタム404ページが表示され、そのままのURLが表示されます。
明らかに私は何かが欠けていますが、それを理解することはできません。「asdf」は実際にはどのコントローラーも指していないので、私の基本コントローラークラスは起動していません。そのため、そこにある「HandleError」フィルターでそれを行うことはできません。
助けてくれてありがとう。
注:ユーザーを404ページにリダイレクトすることは絶対に避けたいです。既存のURLにとどまり、MVCが404VIEWをユーザーにプッシュするようにします。
編集:
私も無駄に次のことを試みました。
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.RouteExistingFiles = False
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("Assets/{*pathInfo}")
routes.IgnoreRoute("{*robotstxt}", New With {.robotstxt = "(.*/)?robots.txt(/.*)?"})
routes.AddCombresRoute("Combres")
''# MapRoute allows for a dynamic UserDetails ID
routes.MapRouteLowercase("UserProfile", _
"Users/{id}/{slug}", _
New With {.controller = "Users", .action = "Details", .slug = UrlParameter.Optional}, _
New With {.id = "\d+"} _
)
''# Default Catch All Valid Routes
routes.MapRouteLowercase( _
"Default", _
"{controller}/{action}/{id}/{slug}", _
New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional} _
)
''# Catch All InValid (NotFound) Routes
routes.MapRoute( _
"NotFound", _
"{*url}", _
New With {.controller = "Error", .action = "NotFound"})
End Sub
私の「NotFound」ルートは何もしていません。