umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.41k stars 2.66k forks source link

Attribute routing has stopped working #16816

Open jcrittenden opened 1 month ago

jcrittenden commented 1 month ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.4.1

Bug summary

There is something in versions >=13.3.1 that broke attribute routing. For instance, I have a UmbracoPageController and the controller applies to all pages versus just the "filtered" routes.

Example: [Host("dev.mysite.com", "qa.mysite.com")] public class MySiteContoller : UmbracoPageController, IVirtualPageController {.....

My temporary solution was to not upgrade past 13.3.0, but the issue persists in 13.4.1 (the newest LTS minor).

Specifics

No response

Steps to reproduce

Apply a route attribute to a page controller, and it will apply to all paths/hosts instead of the filtered ones.

Expected result / actual result

No response

github-actions[bot] commented 1 month ago

Hi there @jcrittenden!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

Migaroez commented 2 weeks ago

Hey @jcrittenden, I have tried to reproduce your issue with the limited reproduction steps you supplied but have been unsuccessful. In the video below you can see that I am able to add the Host attribute to a slightly modified version of the IVirtualPageController example from our documentation. Below that you can find all the code I added to the solution.

Findings: Not supplying the Host attribute, results in the route /products/id hitting the controller Adding the host with my localhost domain, results in the route /products/id hitting the controller Removing the localhost domain from the host attribute but leaving it present with other domains, results in the route not being hit

This seems to be in line with what you expect

https://github.com/user-attachments/assets/39408032-5815-4e6d-afd6-16d85d582545

ProductController and models

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;

namespace ShouldNeverBeDeployed;

[Host("dev.mysite.com", "qa.mysite.com")]
public class ProductController : UmbracoPageController, IVirtualPageController
{
    private readonly IUmbracoContextAccessor _umbracoContextAccessor;

    public ProductController(
        ILogger<UmbracoPageController> logger,
        ICompositeViewEngine compositeViewEngine,
        IUmbracoContextAccessor umbracoContextAccessor
    )
        : base(logger, compositeViewEngine)
    {
        _umbracoContextAccessor = umbracoContextAccessor;
    }

    public IPublishedContent? FindContent(ActionExecutingContext actionExecutingContext)
    {
        var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();

        var productRoot = umbracoContext.Content?.GetAtRoot().First();
        if (productRoot == null)
        {
            return null;
        }

        if (actionExecutingContext.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor)
        {
            return productRoot;
        }
        // Check which action is executing
        return controllerActionDescriptor.ActionName switch
        {
            nameof(Index) => productRoot,
            nameof(Product) => productRoot.Children.First(),
            _ => productRoot
        };
    }

    [HttpGet]
    [Route("products")]
    public IActionResult Index()
    {
        return View("ProductArea",CurrentPage);
    }

    [HttpGet]
    [Route("products/{id}")]
    public IActionResult Product(string id)
    {
        var productItemModel = new ProductModel(CurrentPage)
        {
            Sku = id,
            NotUmbracoData = "This is some extra data",
        };

        return View("ProductDisplay", productItemModel);
    }

    public class ProductModel : ContentModel
    {
        public ProductModel(IPublishedContent content) : base(content)
        {
        }

        public string Sku { get; set; }
        public string NotUmbracoData { get; set; }
    }
}

ProductArea.cshtml

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.ProductArea>
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@{
    Layout = null;
}

@Model.Rich

ProductDisplay.cshtml

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ShouldNeverBeDeployed.ProductController.ProductModel>
@{
    Layout = null;
    var typedUmbracoModel = (ProductDisplay)Model.Content;
}
<h1>@Model.Sku</h1>
@typedUmbracoModel.Disclaimer
<p>@Model.NotUmbracoData</p>