Create an Alias from the Recipe in Orchard

烈酒焚心 提交于 2019-12-10 18:36:17

问题


I want to create the following Alias from the recipe.

How is this achieved?


回答1:


I have created this class to add a command for Orchard to create a new alias from command line or recipe:

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;
        }
    }
}

You can use it in a recipe like this:

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

Put the class inside your module and reference Orchard.Alias



来源:https://stackoverflow.com/questions/17285866/create-an-alias-from-the-recipe-in-orchard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!