2017-08-02 12 views
0

我需要将网站移植到asp.net,并决定使用Umbraco作为底层CMS。Umbraco和根级别的动态URL内容

我遇到的问题是我需要保留当前网站的网址结构。

当前的URL模板看起来如下

domain.com/{brand}/{product} 

这是很难做的,因为它与网站上的所有其他内容混合在一个途径。像domain.com/foo/bar这不是一个品牌或产品。

我编写了一个IContentFinder,并且它注入了一把umbraco管道,该查询的网址结构,如果domain.com/{brand}匹配任何网站上的知名品牌,在这种情况下,我发现它的内部路由的内容determins domain.com/products/并将{brand}/{model}作为HttpContext Items传递并使用IContentFinder返回。

这是有效的,但它也意味着没有调用MVC控制器。所以现在我只是从cshtml文件中的数据库中提取数据,这不是那么漂亮,有点违反MVC约定。

我真正想要的是将网址domain.com/{brand}/{product}重写为domain.com/products/{brand}/{product},然后能够根据参数品牌和产品点击提供内容的ProductsController。

回答

1

有几种方法可以做到这一点。

这取决于您的内容设置。如果您的产品在Umbraco中以页面的形式存在,那么我认为您处在正确的道路上。

在你的内容取景器,记得设置你在这样request.PublishedContent = content;

请求中找到那么你可以采取路由劫持的优势的页面添加一个ProductController的将调用该请求:https://our.umbraco.org/Documentation/Reference/Routing/custom-controllers

实施例实现:

protected bool TryFindContent(PublishedContentRequest docReq, string docType) 
{ 
    var segments = docReq.Uri.GetAbsolutePathDecoded().Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); 
    string[] exceptLast = segments.Take(segments.Length - 1).ToArray(); 

    string toMatch = string.Format("/{0}", string.Join("/", exceptLast)); 

    var found = docReq.RoutingContext.UmbracoContext.ContentCache.GetByRoute(toMatch); 
    if (found != null && found.DocumentTypeAlias == docType) 
    { 
     docReq.PublishedContent = found; 
     return true; 
    } 

    return false; 
} 

public class ProductContentFinder : DoctypeContentFinderBase 
{ 
    public override bool TryFindContent(PublishedContentRequest contentRequest) 
    { 
     // The "productPage" here is the alias of your documenttype 
     return TryFindContent(contentRequest, "productPage"); 
    } 
} 

public class ProductPageController : RenderMvcController {} 

在该示例中的文档类型具有 “productPage” 的别名。这意味着控制器需要完全命名为“ProductPageController”并继承RenderMvcController。

请注意,实际页面名称并不重要。

+0

我只在内容树中的domain.com/Porducts/上有一个页面,我从外部源获取产品。在IContentFinder中,我通过使用ContentCache.GetByRoute(“Products /”)位来设置PublishedContent,但似乎没有通过ProductsController触发,Products内容项具有其自己的文档类型和模板。 我通过在模板上插入Html.RenderAction()并将其指向ProductsController来实现它。但我不认为它非常漂亮,为什么我在调用GetByRoute(“Products /”)时首先无法打到ProductsController。 – CodeMonkey

+0

@CodeMonkey好的,我包含了一个项目的示例代码,我几乎在做你正在寻找的东西。在我的情况下,它会切断url的分段,直到匹配您提供的文档类型。 然后,你可以结合起来,与一个URL重写,那reqrites“品牌/ SKU”到“产品/品牌/ SKU”,和上面的代码应该让你去。 – mortenbock

+0

谢谢,一旦我回家,我会试试这个。ProductPageController的动作如何适用于所有这些?我的Umbraco内容树中的ProductPage内容项是否应该分配模板,还是应该依赖控制器返回视图(cshtml)? – CodeMonkey