Smart search and URL to show pages in MVC

Sylvain C. asked on July 31, 2019 22:52

Hello, I am working on the search interface of my MVC site and followed this documentation. It works fine but I would like to display clickable results to the end user. However, I don't know how to show the URL associated with each result.

I tried to explore the Data property for each result item such as :

<h3>Results for "@Model.Data.Query"</h3>
foreach (SearchResultItem item in Model.Data.Items)
{
    <div>
        <a href="~/@item.Data.GetValue("DocumentNamePath")"> <strong>@item.Title</strong></a>
    </div>
    }

But the DocumentNamePath and NodeAliasPath for example,represent only the position of the page in the tree and do not reflect my MVC URLs. Could the URL pattern of the page type be considered? Is there any other property I could use?

Thank you for your help

Sylvain

Recent Answers


Zach Perry answered on August 1, 2019 22:55

You should be able to add a Page alias under properties on the document in the tree and use that for routing. I believe that field comes back as the nodealiaspath

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 1, 2019 23:03

Thank you Zach Perry. So it means that I need to edit all the documents of my tree (I have few hundreds).
What happen if I do a spelling mistake? I am not sure this solution will be robust enough especially for my editors who will have to make this modification themselves.
Isn't there a way to get the URL pattern or the alternative URL instead to get something more robust?

Regards

Sylvain

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on August 1, 2019 23:04

Not if your tree structure doesn't match your MVC routing.

How are you getting the content from the tree in the MVC app?

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 1, 2019 23:12

In the General properties of the page, there is the live URL based on the URL pattern of the page type. This property would be fine.
For the time being, I am getting the content using the URL pattern or alterntive URLS which are matching with relevant controllers (/{%DocumentCulture%}/Content/Detail/{%NodeAlias%} for example).
Is it part of the best practise to have your tree reflecting your MVC routing?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.