Get Previous Page / Or page that Triggered the 404 page to appear

Vote:
 

Hi,

I Am trying to get the page that redirected to my ErrorPage, so i can do things with it :

it should be somethink Like this

 public ActionResult Index(ErrorPage currentPage)
        {

var previousPage/ or PageThatRedirectedUsTo404Page  = currentPage...........; Here is the problem, 

            if (previousPage !=null &&
 previousPage.StopPublish < DateTime.Now)
            {
                Response.Status = "410 Gone";
                Response.StatusCode = 410;
                Response.Write(Response.Status);
                Response.End();
                return View(currentPage.DefaultViewPath, _pageViewModelFactory.CreateViewModel(currentPage));
            }
                .....
.......
.....
         }
           

Am new to backend Dev. and new to Epi. Has anyone tried to solve this before? Any Ideas? 

Thnx,

// Rajaa

#192194
May 09, 2018 16:18
Vote:
 

How do you redirect to the error page?

Our solution has been to add this to the web.config httpErrors section:
<error statusCode="404" path="/404" responseMode="ExecuteURL" />

And registered a route for /404:

routes.MapRoute("404","404",new { controller = "Error", action = "NotFound" });

Inside the ErrorController we would still have the original info as we are still processing the same request from the client:

public ActionResult NotFound()
{
    var pathAndQuery = Request.Url.PathAndQuery.ToLowerInvariant();
    ...
}

There might very well be nicer solutions out there though. :)

#192196
May 09, 2018 17:26
Vote:
 

Hi Rajaa

You could take a look or even use the popular open source 404 handler for Episerver:

https://github.com/Geta/404handler

David

#192256
May 11, 2018 22:08
Vote:
 

Hi, 

Thank you for the help! 

https://github.com/Geta/404handler was very good. 

I redirect this way : 

<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="410" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Common/404/" responseMode="ExecuteURL" />
<error statusCode="410" prefixLanguageFilePath="" path="/Common/410/" responseMode="ExecuteURL" />
</httpErrors>

I have tried the 

var pathAndQuery = Request.Url.PathAndQuery.ToLowerInvariant();

but then it gave me a string of a url, which seemed to be an impossible way to retreive a Pagedata going this way :(  Am I wrong? 

Am using the same ActionResult Method for all errors, 404, 410..and so on. the same viewModel and View. My goal wa to determine if the errorPage was triggered by a neverExisted Page, or by a page that has expired. coz i want to do differents things depending on the reason for the redirect. i Really hoped to get to the errorTriggererPage, using the current page (ErrorPage). 

Thank you again:-)

// Rajaa

#192266
May 14, 2018 0:16
Vote:
 
_urlResolver.Route(new UrlBuilder(url));

Normally gets you the PageData, but it probably won't work for an expired page, you can try to add a ContextMode as the second parameter to see if it helps.

#192277
May 14, 2018 11:17
Vote:
 

Hello!1 

Thnk you guys, really helpful! 

I was over thinking it! 

here is part of the solution: 

 

   if (language != null && errorLanguage != null &&

                string.Compare(language.Name, errorLanguage.Name,
            StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                ErrorPage currentSiteerrorPage = _contentLoader.Get<ErrorPage>
                    (currentPage.ContentLink, language);               

                if (currentSiteerrorPage != null)
                {
                    if (pathAndQuery.ToLower().Contains("/archive"))  //  && !pathAndQuery.Contains("/oldpage") 
                    {
                        Response.Status = "410 Gone";
                        _context.HttpContext.Response.StatusCode = 410;
                        return View(currentSiteerrorPage.DefaultViewPath, _pageViewModelFactory.CreateViewModel(currentSiteerrorPage));
                    }
                    _context.HttpContext.Response.StatusCode = 404;
                    return View(currentSiteerrorPage.DefaultViewPath, _pageViewModelFactory.CreateViewModel(currentSiteerrorPage));
                }
            }

Thank you1 

// Rajaa

#192288
May 14, 2018 15:26
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.