Url.Action does not work in CMS 12?

Vote:
 

Hi!

We have just upgraded to CMS 12 and we have this in our view:

<form id="searchbox" action="@Url.Action("Index", "SearchPage")" method="get" autocomplete="off" class="page-searchbox tracked-search">

In CMS 11 I get this: 

<form id="searchbox" action="/sv/admin/soksida" method="get" autocomplete="off" class="page-searchbox tracked-search">

which is correct, but in CMS 12 I get this:

<form id="searchbox" action="/modules/App/SearchPage" method="get" autocomplete="off" class="page-searchbox tracked-search">

What am I missing?

Thanks!

/Kristoffer

#286055
Aug 24, 2022 9:07
Vote:
 

UPDATE:

I added:

_ = endpoints.MapControllerRoute(name: "Default", pattern: "{controller}/{action}/{id?}");

and that gave me:

<form id="searchbox" action="/SearchPage/Index" method="get" autocomplete="off" class="page-searchbox tracked-search">

Still missing something.

Come to think aobut it, I think it is more strange that it works in CMS 11? Shouldn't 

action="@Url.ContentUrl(Model.CurrentPage.ContentLink)"

be tha way to go?

/Kristoffer

#286061
Edited, Aug 24, 2022 11:10
Vote:
 

Kristoffer I was looking into this myself and believe its due to the way the Middle Layer for routing has changed in .Net Core. As you mentioned it might be better switching to Url.ContentURL or if you have a view model at your disposle store the URL in a property. 

@{ using (Html.BeginForm(null, null, Html.ViewContext.IsInEditMode() ? FormMethod.Post : FormMethod.Get, new { @action = Model.Layout.SearchActionUrl }))
    {
        <input type="text" class="search-query" name="q" id="SearchKeywords" placeholder="@Html.Translate("/mainnavigation/search")" />
        <input type="submit" class="searchButton" id="SearchButton" value="" />
    }
}

Or 

        <div class="searchContainer" id="searchBar">
            <form class="searchWrap" method="get" action="@Url.ContentUrl(Model.SearchResultsPage)#results-top">

                <div class="searchForm">
                    <label for="searchSite" class="offscreen">Search site</label>
                    <input type="text" id="searchSite" maxlength="50" name="@Configuration.Constants.QueryString.Query" placeholder="@Model.SearchBoxPlaceholderText" />
                    <button type="submit" class="icon searchButton">
                        <svg>
                            <title>@Model.SearchText</title>
                            <use xlink:href="#svg-search"></use>
                        </svg>
                    </button>
                </div>
            </form>
#286065
Aug 24, 2022 13:39
Vote:
 

Hi Minesh!

Yes, I will solve it in some of the ways above, it will work just fine. I just wanted to know that is does not work and that I'm not just missed to add something.
I will start like that and we will see.

Thanks!

/Kristoffer

#286067
Aug 24, 2022 16:59
Vote:
 

The reason is becasue of how middleware works and the order of exceution changed between the net 5/ 6 and full framework.  Using contenturl is the way to go.

#286068
Aug 24, 2022 21:09
Vote:
 

Have you tried using @Html.BeginContentForm instead? I think it's a new addition to CMS 12.  It has a lot of methods so I created an extension method to simplify it in a couple of places:

public static class HtmlHelperExtensions
{
    public static MvcForm BeginContentForm<T>(
        this IHtmlHelper<T> htmlHelper, 
        ContentReference? contentReference,
        FormMethod formMethod,
        string formClass)
    {
        return htmlHelper.BeginContentForm(
            contentReference, 
            CultureInfo.CurrentUICulture.Name, 
            null,
            formMethod, 
            false, 
            new { @class = formClass });
    }
}

Then on the razor file I have this:

@using (Html.BeginContentForm(Model.SiteSettings.SearchPage, FormMethod.Get, "footer-search__form"))
{
    <div class="p-footer__input-group">
        <input type="text"
               name="query"
               placeholder="@Model.SiteSettings.FooterSearchPlaceholder"
               aria-label="@Model.SiteSettings.FooterSearchPlaceholder"
               autocomplete="off"
               required />
        <button type="submit" class="footer-search__button">Search</button>
    </div>
}
#286171
Aug 26, 2022 8:02
Vote:
 

I'm running into this as well for Url.Action no longer working with content urls. I think the best bet in many situations may be to inject IUrlResolver in your view and call getUrl with VirtualPathArguments as you can set Action on it. Perhaps a UrlHelper extension might be a good idea for syntactic sugar.

Unfortunately, there are cases were link generation might not be in your complete control, so I would love to see content aware implementations of IUrlHelper and LinkGenerator

#288260
Sep 28, 2022 20:51
* 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.