2013-06-24 6 views
3

Я хочу создать следующий псевдоним из рецепта.Создайте псевдоним из рецепта в саду

alias

Как это достигается?

+0

Можете ли вы быть более конкретным? Какой рецепт? Я не думаю, что есть шаг рецепта, который может это сделать, и псевдонимы не являются предметами контента и, как таковые, по своей сути не являются импортируемыми. Вероятно, вам придется написать свою собственную команду или шаг рецепта. –

+0

То, что я действительно хочу достичь, - это указать страницу по умолчанию для конкретного контроллера и действия в настраиваемом модуле, а не на обычной странице. – carrier

+0

@carrier Я пытаюсь сделать то же самое. Вы выяснили способ или напишите команду или рецепт, чтобы достичь этого? – Darlene

ответ

0

Я создал этот класс, чтобы добавить команду для Orchard, чтобы создать новый псевдоним из командной строки или рецепта:

using Orchard; 
using Orchard.Alias; 
using Orchard.Commands; 
using System; 
using Orchard.Environment; 
using System.Linq; 

namespace Contrib.Foundation.Common.Commands 
{ 
    public class AliasCommands : DefaultOrchardCommandHandler 
    { 
     private readonly Work<WorkContext> _workContext; 
     private readonly IAliasService _aliasService; 

     public AliasCommands(Work<WorkContext> workContext, IAliasService aliasService, 
      IOrchardServices orchardServices) 
     { 
      _workContext = workContext; 
      _aliasService = aliasService; 
      Services = orchardServices; 
     } 
     public IOrchardServices Services { get; private set; } 

     [OrchardSwitch] 
     public string AliasPath { get; set; } 
     [OrchardSwitch] 
     public string RoutePath { get; set; } 

     [CommandName("alias add")] 
     [CommandHelp("alias add /AliasPath:<alias-path> /RoutePath:<route-path>\r\n\t" + "Add a new alias")] 
     [OrchardSwitches("AliasPath,RoutePath")] 
     public void Add() 
     { 
      AliasPath = AliasPath.TrimStart('/', '\\'); 
      if (String.IsNullOrWhiteSpace(AliasPath)) 
      { 
       AliasPath = "/"; 
      } 
      if (String.IsNullOrWhiteSpace(RoutePath)) 
      { 
       Context.Output.WriteLine(T("Route can't be empty")); 
       return; 
      }  
      if (CheckAndWarnIfAliasExists(AliasPath)) 
      { 
       Context.Output.WriteLine(T("Alias already exist")); 
       return; 
      } 
      try 
      { 
       _aliasService.Set(AliasPath, RoutePath, "Custom"); 
      } 
      catch (Exception ex) 
      { 
       Services.TransactionManager.Cancel(); 
       Context.Output.WriteLine(T("An error occured while creating the alias {0}: {1}. Please check the values are correct.", AliasPath, ex.Message)); 
       return; 
      } 
      Context.Output.WriteLine(T("Alias {0} created.", AliasPath)); 
     } 
     private string GetExistingPathForAlias(string aliasPath) 
     { 
      var routeValues = _aliasService.Get(aliasPath.TrimStart('/', '\\')); 
      if (routeValues == null) return null; 

      return _aliasService.LookupVirtualPaths(routeValues, _workContext.Value.HttpContext) 
       .Select(vpd => vpd.VirtualPath) 
       .FirstOrDefault(); 
     } 
     private bool CheckAndWarnIfAliasExists(string aliasPath) 
     { 
      var routePath = GetExistingPathForAlias(aliasPath); 
      if (routePath == null) return false; 

      return true; 
     } 
    } 
} 

Вы можете использовать его в рецепте, как это:

<Command> 
alias add /AliasPath:"/" /RoutePath:"mycontroller" 
</Command> 

Поместите класс внутри своего модуля и ссылку Orchard.Alias ​​