5

私の質問は、この投稿に対する Pure.Kromes の回答に関するものです。彼の方法を使用してページのカスタム エラー メッセージを実装しようとしましたが、うまく説明できない問題がいくつかあります。

a) localhost:3001/NonexistantPage などの無効な URL を入力して 404 エラーを発生させると、NotFound() に移動する必要があるにもかかわらず、エラー コントローラーの ServerError() アクションにデフォルト設定されます。これが私のErrorControllerです:

    public class ErrorController : コントローラ
    {
        public ActionResult NotFound()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            var viewModel = 新しい ErrorViewModel()
            {
                ServerException = Server.GetLastError(),
                HTTPStatusCode = Response.StatusCode
            };
            ビューを返します(viewModel);
        }

        public ActionResult ServerError()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            var viewModel = 新しい ErrorViewModel()
            {
                ServerException = Server.GetLastError(),
                HTTPStatusCode = Response.StatusCode
            };
            ビューを返します(viewModel);
        }
    }

Global.asax.cs のエラー ルート:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            name: "Error - 404",
            url: "NotFound",
            defaults: new { controller = "Error", action = "NotFound" }
            );

        routes.MapRoute(
            name: "Error - 500",
            url: "ServerError",
            defaults: new { controller = "Error", action = "ServerError" }
            );


そして私のweb.config設定:

<system.web>
    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ServerError">
    <error statusCode="404" redirect="/NotFound" />
</customErrors>
...
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...

エラー ビューは、NotFound.cshtml および ServerError.cshtml として /Views/Error/ にあります。

b) 面白いことに、サーバー エラーが発生すると、実際には定義したサーバー エラー ビューが表示されますが、エラー ページが見つからないというデフォルトのエラー メッセージも出力されます。

これがどのように見えるかです:


これら2つの問題を解決する方法について何かアドバイスはありますか? これらのエラー メッセージを実装する Pure.Kromes のアプローチが本当に気に入っていますが、これを達成するためのより良い方法があれば、遠慮なく教えてください。

ありがとう!

**EDIT : ** /Error/NotFound または Error/ServerError にアクセスすることで、ErrorController を介してビューに直接移動できます。

ビュー自体にはテキストのみが含まれ、マークアップなどは含まれません。

私が言ったように、それは実際には何らかの形で機能しますが、私が意図した方法とは異なります. web.config のリダイレクトに問題があるようですが、私はそれを理解できませんでした。

4

2 に答える 2

2

より複雑なルートがあり、いくつかのセグメントがある場合、そのセットアップにはもう1つの問題があります。

http://localhost:2902/dsad/dadasdmasda/ddadad/dadads/ddadad/dadadadad/

サーバーエラーが発生しました ->

Sorry, an error occurred while processing your request.


Exception: An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: ~/Views/Error/NotFound.cshtml; Message: The RouteData must contain an item named 'controller' with a non-empty string value.
Source: 

そのための私の解決策は、デフォルトルートの後に最後に追加のルートを追加することでした

        routes.MapRoute(
            "Default Catch all 404",
            "{controller}/{action}/{*catchall}",
            new { controller = "Error", action = "NotFound" }
        );

それが誰かを助けることを願っています:-)

于 2012-12-19T17:02:02.033 に答える
0

私はそれを働かせました。そもそも問題に対する私の理解が少し間違っていたようです。

web.config で、次のように変更しました。

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Error/ServerError.cshtml">
  <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>

... と ...

<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
    </httpErrors>

これにより、ビューに直接リダイレクトされます。私の理解では、ビューにリダイレクトされるエラーコントローラーにリダイレクトする必要がありましたが、明らかにそうではありませんでした。カスタムエラーを捨てて、単に怠惰になってYSODを表示しようとしていたときに、再び問題を分析させてくれたコメントに感謝します。:)

于 2012-11-12T08:33:13.093 に答える