views:

90

answers:

0

Would it make sense to create the following custom ActionResults? Why or why not?

NotFoundResult

Currently, if an action method encounters a scenario where it needs to send a "not found" page to the user, I call this method, which is defined in my base controller class:

    protected ViewResult PageNotFound(string internalErrorMessage)
    {
        Trace.Write(String.Format("PageNotFound: \"{0}\"", internalErrorMessage), GetType().Name);


        Infrastructure.Log.Info(String.Format("PageNotFound: \"{0}\"", internalErrorMessage));


        Response.StatusCode = (int)HttpStatusCode.NotFound;


        // TODO: Set up contextual "not found" error view pages

        var model = BuildNotFoundErrorViewModel();

        return View("~/Views/Error/NotFound.aspx", model);        
    }

Wouldn't it be cleaner to simply return a "NotFoundViewResult" instead, which would take care of setting the response code, finding the view, etc?

ApplicationErrorResult

Currently, if an action method encounters a scenario where it needs to crash (due to some kind of internal inconsistency that can't be recovered from), I do the following:

        if (parameters.HasParseErrors)
        {
            var action = CatalogueRequestUtility.GetActionForRequest(parameters);

            switch (action)
            {
                case CatalogueRequestParseErrorAction.ThrowException:
                    throw new ApplicationException(
                        String.Format(
                            "Parsing the CatalogueRequestCollection for the current request resulted in a fatal exception. Error List: \"{0}\".",
                            String.Join(", ", parameters.ParseErrors.Select(e => e.ErrorMessage).ToArray())
                        )
                    );
            }
        }

Would it be cleaner to just return a "ApplicationErrorResult" instead?

My motivation for these custom results are:

  • The contoller is simplified
  • A lot of code sharing can happen - controllers can just call a utility method that returns the appropriate action result
  • The various exit points from a controller are clear and well defined - even if the application needs to throw a not found or error response

Thanks in advance for any input!