Copied to clipboard

Flag this post as spam?

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


  • npack 21 posts 203 karma points
    Jun 06, 2024 @ 17:15
    npack
    0

    Virtual Page Controller - How to return my custom 404 page?

    I've derived from UmbracoPageController, IVirtualPageController as seen on https://docs.umbraco.com/umbraco-cms/reference/routing/custom-routes, including an entry in appSettings.json for "ReservedPaths" which takes me out of the umbraco pipeline.

    The following code in my controller is working great, but the 'return NotFound()' results in a raw 404 instead of my configured custom 404 page. My custom 404 page works everywhere else, but because of the "ReservedPaths" it doesn't get used here. Is there some way for me to include a content node along with my 404 action result? I've seen some references to 'SetPublishedContent' but maybe they were for previous versions of umbraco or I don't know how to use it.

    [HttpGet]
    public async Task<IActionResult> Index(string id)
    {
        var newsArticle = await myApi.RetrieveNewsArticle(id);
    
        if (newsArticle != null)
        {
            var model = new NewsArticleViewModel(CurrentPage, this.PublishedValueFallback);
            model.NewsArticle = newsArticle;
            return View(CurrentPage!.GetTemplateAlias(), model);
        }
    
        return NotFound(); // this is currently resulting in an unstyled 404 page, not the umbraco 'not found' page
    }
    

    What I'd like to do: (doesn't compile)

    const int NOT_FOUND_PAGE_ID = 2241;
    var notFoundPage = umbracoContext.Content?.GetById(NOT_FOUND_PAGE_ID);
    Request.SetPublishedContent(notFoundPage);
    
  • npack 21 posts 203 karma points
    Jun 06, 2024 @ 17:27
    npack
    101

    Hey,

    I had an epiphany and figured it out after looking at my posted question. Pretty obvious perhaps but I'm still a rookie.

    For anybody else:

    const int NOTFOUNDPAGE_ID = 2241;

    var notFoundPage = umbracoContext.Content?.GetById(NOTFOUNDPAGE_ID);

    Response.StatusCode = 404;

    return View(notFoundPage!.GetTemplateAlias(), notFoundPage);

Please Sign in or register to post replies

Write your reply to:

Draft