Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Chart 15 posts 35 karma points
    Sep 02, 2013 @ 16:22
    Michael Chart
    0

    MVC - return 404 from child action

    I'm using MVC in Umbraco 6. I am trying to get the configured Umbraco 404 page to render following some condition in a child action.

    I'm calling a child action from one of my templates:

    @Html.Action("ShowBoatDetails", "Boat");

    Here's the child action in the controller:

    public class BoatController : SurfaceController
    {
    [ChildActionOnly]
    public ActionResult ShowBoatDetails()
    {
    if (SomeCondition)
    {
    return HttpNotFound();
    } var model = new BoatModel(); return PartialView("BoatDetails", model);
    }
    }

    It is when SomeCondition is true, I am trying to get the child action to render the configured umbraco 404 page. What is currently happening is that a 404 status code is returned, but the rest of the page continues to render as normal - the umbraco 404 page is not shown.

    Edit

    I set my web.config to the following:

        <httpErrors existingResponse="Replace" errorMode="Custom">
          <remove statusCode="404"  />
          <error statusCode="404" path="/foobar" responseMode="ExecuteURL" />
        </httpErrors>

    And the 404 now renders correctly when called from the child action, however 500 server errors are now showing a very basic error page with no error details (not the normal YSOD).

  • Charles Afford 1163 posts 1709 karma points
    Sep 02, 2013 @ 18:53
    Charles Afford
    0

    You can either add a error statusCode for the 500

    When you say basic errors what are these?  You can this in IIS.  or could catch some error in your controller and set the status to 500 and return a responce.  Charlie :)

  • Michael Chart 15 posts 35 karma points
    Sep 03, 2013 @ 10:35
    Michael Chart
    0

    Thanks for the response. I was just seeing the default IIS 500 error page rather than a YSOD with error details.

     

    So I have decided to, as you suggest, add a 500 error statusCode.

    <httpErrors existingResponse="Replace" errorMode="Custom">
    <remove statusCode="404"  />
    <remove statusCode="500"  />
    <error statusCode="404" path="/foobar" responseMode="ExecuteURL" />
    <error statusCode="500" path="/500.html" responseMode="ExecuteURL" />
    </httpErrors>

    However, I decided it was best just to add this only in release mode for production so that I can still see YSODs in debug mode.

    Thanks,

    Michael

     

     

  • Charles Afford 1163 posts 1709 karma points
    Sep 03, 2013 @ 11:09
    Charles Afford
    0

    Right, well you could handel tthe 500 error in your code for instance. I believe the problem is the YSOD error is futher down the call stack than those custom errors. You can call Server.GetLastError() and then throw that error when you come across a 500? Never tried this but in theroy it works. Charlie

  • Michael Chart 15 posts 35 karma points
    Sep 03, 2013 @ 11:18
    Michael Chart
    0

    The thing is I don't know where the 500 errors might come up at this stage, so for debugging purposes the YSOD is helpful. Or are you thinking of some kind of global catchall for 500 errors? Do you think that it might get to a recursive situation where it catches a 500 and then throws it, then catches it and throws it...etc

  • Charles Afford 1163 posts 1709 karma points
    Sep 03, 2013 @ 11:43
    Charles Afford
    0

    It what situation did you you consider a recursive situation? If you had it in your model and you threw a new error passing in the Server.GetLastError(), then the error should be displayed?

Please Sign in or register to post replies

Write your reply to:

Draft