Developer forum

Forum » Ecommerce - Standard features » Redirect to 404 when no product from url

Redirect to 404 when no product from url

Kasper Legarth
Reply

How would i achieve to make the page redirect to the 404 page of the area, when not finding the specified product in the URL?

The request returns 404. But no redirect is happening.


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Kasper

There will be no redirects - it will just send out a 404 status with the content of that page.

In this case the 404 return comes from within the product catalog - and it seems something is not picking up the correct page/website 404 and returns a 'regular' 404 which is basically just the response code that was changed.

Will look into it.

Votes for this answer: 1
 
Kasper Legarth
Reply

To make sure I'm getting what you are saying.

It should be showing the 404 page that is set up on the website?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Yes.

 
Kasper Legarth
Reply

Hi Nicolai

Just curios. Are there any updates on this? Has it been planed in an upcoming sprint, or is it still in the backlog? :) 

 
Kasper Legarth
Reply

Hello.

Any updates on this one? :) 

 
Kasper Legarth
Reply

Sorry to keep nagging.

Is there any update on this?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Kasper

There are 2 things to this.

First one is the exception you receive - that was fixed in this pull request:

https://github.com/dynamicweb/Swift/commit/172ebd7f6301f8b2cb77aee521a39c9573d38bc1

In your solution it is a null check that needs to bere there:

That will take care of the exception.

The page will then return 404, but not show the content of the 404 page. We have a task to find a solution to that. It comes with a bit of complexity why it has not yet been fixed, but we are on it.

As a temporary workaround you can add this notification subscriber if you have a custom project:

/// <summary>
/// Represents the notification subscriber for page load.
/// </summary>
[Subscribe(Standard.Page.Loaded)]
public class EcomPageLoadNotificationSubscriber : NotificationSubscriber
{

    /// <summary>
    /// Called when notification is broadcast.
    /// </summary>
    /// <param name="notification">The notification.</param>
    /// <param name="args">The arguments.</param>
    public override void OnNotify(string notification, NotificationArgs args)
    {
        Standard.Page.LoadedArgs loadedArgs = (Standard.Page.LoadedArgs)args;
        PageView pageview = loadedArgs.PageViewInstance;

        if (Context.Current.Request.QueryString["ProductId"] is string productId)
        {
            if (Services.Products.GetProductById(productId, string.Empty, true) is null)
            {
                loadedArgs.OutputResult = new NotFoundOutputResult();
                return;
            }
        }
        if (Context.Current.Request.QueryString["GroupId"] is string groupId)
        {
            if (Services.ProductGroups.GetGroup(groupId, Common.Context.LanguageID) is null)
            {
                loadedArgs.OutputResult = new NotFoundOutputResult();
                return;
            }
        }
    }
}
 
Alexandru Aliu
Reply

Hey guys, I'm trying to achieve this in 9.18 but it seems like OutputResult and NotFoundOutputResult() are not available anymore. Is there an other way to do it ?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Alexandry

OutputResult is a DW10 thing, so you cannot do that.

Instead you can do something like this:

response.ClearHeaders();
response.StatusCode = 404;
response.End();

 

You must be logged in to post in the forum