现在的位置: 首页 > 综合 > 正文

OrchardHUN.TrainingDemo 学习记录(1)-Controller中,使用Orchard API

2013年06月17日 ⁄ 综合 ⁄ 共 1333字 ⁄ 字号 评论关闭

1、Controller中,使用Orchard API.例子:

using Orchard;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.UI.Notify;
using System.Web.Mvc;

namespace OrchardHUN.TrainingDemo.Controllers
{
    public class DependencyInjectionController :Controller
    {
        private readonly INotifier _notifier;

        private readonly IWorkContextAccessor _wca;
        private readonly IHttpContextAccessor _hca;
        public Localizer T { get; set; }

        public ILogger Logger { get; set; }

        public DependencyInjectionController(INotifier notifier, IWorkContextAccessor wca, IHttpContextAccessor hca)
        {
            _notifier = notifier;
            _wca = wca;
            _hca = hca;
            T = NullLocalizer.Instance;
            Logger = NullLogger.Instance;
        }
        public ActionResult NotifyMe()
        {
            //当前用户
            var currentUser = _wca.GetContext().CurrentUser;
            //当前用户名称
            var currentUserName = currentUser == null ? "<subject name here>" : currentUser.UserName;
            //当前URL
            var currentUrl = _hca.Current().Request.Url.ToString();
            //在用户界面出现消息

            _notifier.Information(T("Please continue testing. -- Subject name: {0}; Subject location: {1}", currentUserName, currentUrl));

           //在日志中写入消息,消息存放在以日期为单位,类型为error的信息文本文件中 App_Data/Logs!
            Logger.Error("Test subject notified.");

            // We redirect to the first controller here but the notification will still be displayed. This is because notifications
            // are meant to provide a way to interact with the user even after a redirect. Of course after they're shown once 
            // notifications will be dismissed.
            return RedirectToAction("Index", "YourFirstOrchard");
        }
    }
}

 

 

 

 

 

抱歉!评论已关闭.