Copied to clipboard

Flag this post as spam?

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


  • Philip Hayton 98 posts 435 karma points
    Oct 26, 2019 @ 14:16
    Philip Hayton
    1

    Return Umbraco 404 from RenderMvcController

    Hi guys,

    Is it possible, and if so how do you return the standard Umbraco 404 error from a RenderMvcController? This beautiful bad boy:

    enter image description here

    In my scenario, I am using a UmbracoVirtualNodeRouteHandler to handle a custom route. A RenderMvcController is responsible for pulling the content from a remote API via ID. If the content doesn't exist, I'd like to return the same 404 page generated by normal Umbraco routing.

    Here's how I'm currently doing it. This returns an IIS error page though.

            var remoteContent = await _service.Get(id);
            if (remoteContent == null) return new HttpNotFoundResult();
    

    Any help is greatly appreciated. TIA!

  • Sasa Mladenovic 2 posts 72 karma points
    1 week ago
    Sasa Mladenovic
    0

    Hey Philip, did you manage to resolve this? I need the same thing.

  • npack 66 posts 374 karma points
    6 days ago
    npack
    0

    I struggled for a bit returning umbraco's configured not found page from my own virtual page controllers.

    I'm not sure if our situation is the same (I'm on v15) but this is what I ended up with in case you are out of ideas.

    return BrandedNotFound();
    
    
    protected IActionResult BrandedNotFound()
    {
    
        var notFoundPageKey = Guid.Parse("6ef29dbd-a6e9-4d8f-b72e-99a103361280");
    
        var notFoundPage = umbracoHelper.Content(notFoundPageKey);
        Response.StatusCode = 404;
        if (notFoundPage != null)
        {
            return View(notFoundPage!.GetTemplateAlias(), notFoundPage);
        }
    
        return NotFound(); // Unstyled
    
    }
    

    Where that guid is the one configured in my appsettings.json

    "Error404Collection": [
      {
        "Culture": "default",
        "ContentKey": "6ef29dbd-a6e9-4d8f-b72e-99a103361280"
      }
    ]
    
  • Sasa Mladenovic 2 posts 72 karma points
    4 days ago
    Sasa Mladenovic
    0

    Thanks npack,

    We already did something similar. I will add it here in case someone finds it useful:

    public override IActionResult Index()
    {
        if (IsExpiredJob())
        {
            return HttpNotFoundPage();
        }
    
        return CurrentTemplate(CurrentPage);
    }
    
    private ViewResult HttpNotFoundPage()
    {
        var home = CurrentPage.Root<HomePage>();
        Response.Clear();
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        var error404 = home.DescendantOrSelf<Error404>();
    
        var contentModel = new ContentModel(error404);
        var viewData = new ViewDataDictionary(
            new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(),
            new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()
        )
        {
            Model = contentModel
        };
    
        var viewResult = new ViewResult
        {
            ViewData = viewData,
            ViewName = contentModel.Content.GetTemplateAlias(),
        };
    
        return viewResult;
    }
    

    It's for version 13 though.

Please Sign in or register to post replies

Write your reply to:

Draft