Crafty
Crafty

Reputation: 11

MapDynamicControllerRoute results in "Page not found"

Code in an OrchardCore environment

I'm currently building a feature to allow my clients to create more custom URLs for their sitemap.

I've made a setting where the client can choose the prefix, keywords and locations.

An example could be: ~/what-do-we-do/programming/antwerp

I've tried to do it the same way as the homepage with a DynamicRouteValueTransformer:

In my startup.cs:

[Feature("OrchardCore.Sitemaps")]
public class SitemapStartUp: StartupBase
{
    public override int Order => -50;

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddContentPart<CustomSiteMapSettings>();
        services.AddSingleton<KeywordLocationTransformer>();
    }

    public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
    {
        var siteSettings = serviceProvider.GetRequiredService<ISiteService>().LoadSiteSettingsAsync().GetAwaiter().GetResult();
        var customSitemapSettings = siteSettings.As<ContentItem>("CustomSiteMapSettings").As<CustomSiteMapSettings>();

        if (customSitemapSettings != null)
        {
            var prefix = customSitemapSettings.Prefix.Text;

            routes.MapDynamicControllerRoute<KeywordLocationTransformer>("/"+prefix+"/{keyword}/{location}");
        }
    }
}

In my KeywordLocationTransformer:

public class KeywordLocationTransformer : DynamicRouteValueTransformer
{
    private readonly ISiteService _siteService;

    public KeywordLocationTransformer(ISiteService siteService)
    {
        _siteService = siteService;
    }

    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
    {
        var customSiteMapSettings = (await _siteService.GetSiteSettingsAsync()).As<ContentItem>("CustomSiteMapSettings").As<CustomSiteMapSettings>();

        var keyword = values["keyword"] as string;
        var location = values["location"] as string;

        if (!customSiteMapSettings.Keywords.Text.Contains(keyword))
            return null;

        if (!customSiteMapSettings.Locations.Text.Contains(location))
            return null;

        var route =  new RouteValueDictionary()
        {
            {"Area", "Crafty.OrchardCore.Foundation" },
            {"Controller", typeof(KeywordLocationController).ControllerName() },
            {"Action", nameof(KeywordLocationController.Index) }
        };

        return route;
    }
}

My KeywordLocationController:

public class KeywordLocationController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

When I go to the URL above the routevalue is created and I was expecting it would go to my custom controller. Instead it showed me "Page could not be found"

Just for making sure I did everything well I implemented the same code as the HomeRouteTransformer and my route now shows the homepage as expected.

It seems my custom controller is not reachable. What do I do wrong?

Thanks

Upvotes: 0

Views: 35

Answers (0)

Related Questions