public async Task<string> CreatePageForIncidentAsync(string siteRootDirectory, string sectionId, Incident incident, IEnumerable<FileContent> inspectionPhotos, IEnumerable<Video> incidentVideos)
        {
            var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml");
            var template = System.IO.File.ReadAllText(templateFile);
            var viewBag = new RazorEngine.Templating.DynamicViewBag();
            viewBag.AddValue("InspectionPhotos", inspectionPhotos);
            viewBag.AddValue("IncidentVideos", incidentVideos);

            var html = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag);
            var content = new MultipartFormDataContent();
            content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation");

            foreach (var image in inspectionPhotos)
            {
                var itemContent = new ByteArrayContent(image.Bytes);
                var contentType = MimeMapping.GetMimeMapping(image.Name);
                itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                content.Add(itemContent, image.Id);
            }

            this.pageEndPoint = string.Format("{0}/sections/{1}/pages", serviceBaseUrl, sectionId);
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint);
            requestMessage.Content = content;

            var responseMessage = await HttpSendAsync(requestMessage, accessToken);
            if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
                throw new HttpResponseException(responseMessage.StatusCode);

            var reponseObject = await GetReponseObjectAsync(responseMessage);
            return (string)reponseObject.links.oneNoteWebUrl.href;
        }
        public override DynamicViewBag GetViewBag() {
            DynamicViewBag bag = new DynamicViewBag();

            bag.AddValue("Title", "Razor in Starcounter");
            bag.AddValue("Products", Db.SQL("SELECT p FROM Simplified.Ring3.Product p ORDER BY p.Name"));

            return bag;
        }
        private DynamicViewBag getInitViewBag()
        {
            var viewBag = new DynamicViewBag();
            viewBag.AddValue("HelpRoute", HelpRoute);  //将Help路由 传递到View

            return viewBag;
        }
Example #4
0
        /// <summary>
        /// When overriden returns ViewBag for Razor view.
        /// </summary>
        /// <returns></returns>
        public virtual DynamicViewBag GetViewBag() {
            DynamicViewBag bag = new DynamicViewBag();

            bag.AddValue("Title", "Default page title");

            return bag;
        }
        public async static Task <string> CreatePageForIncidentAsync(GraphServiceClient graphService, string siteRootDirectory, Group group, Section section, Incident incident, IEnumerable <FileContent> inspectionPhotos, IEnumerable <Models.Video> incidentVideos)
        {
            var accessToken  = AuthenticationHelper.GetGraphAccessTokenAsync();
            var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml");
            var template     = System.IO.File.ReadAllText(templateFile);
            var viewBag      = new RazorEngine.Templating.DynamicViewBag();

            viewBag.AddValue("InspectionPhotos", inspectionPhotos);
            viewBag.AddValue("IncidentVideos", incidentVideos);

            var html    = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag);
            var content = new MultipartFormDataContent();

            content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation");

            foreach (var image in inspectionPhotos)
            {
                var itemContent = new ByteArrayContent(image.Bytes);
                var contentType = MimeMapping.GetMimeMapping(image.Name);
                itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                content.Add(itemContent, image.Id);
            }

            var pageEndPoint   = string.Format("{0}groups/{1}/notes/sections/{2}/pages", AADAppSettings.GraphBetaResourceUrl, group.Id, section.id);
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint);

            requestMessage.Content = content;

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await accessToken);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var responseMessage = await client.SendAsync(requestMessage);

                if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    throw new HttpResponseException(responseMessage.StatusCode);
                }

                var payload = await responseMessage.Content.ReadAsStringAsync();

                return(JObject.Parse(payload)["links"]["oneNoteWebUrl"]["href"].ToString());
            }
        }
        //методы
        public virtual string Transform(string templateName, Dictionary<string, string> replaceStrings)
        {
            var viewBag = new DynamicViewBag();
            foreach (KeyValuePair<string, string> item in replaceStrings)
            {
                viewBag.AddValue(item.Key, item.Value);
            }

            return Engine.Razor.RunCompile(templateName, viewBag: viewBag);
        }
        public async Task Invoke(HttpContext context)
        {
            var requestPath = context.Request.Path.Value;
            if (!String.IsNullOrWhiteSpace(requestPath) &&
                requestPath.Equals("/", StringComparison.CurrentCultureIgnoreCase))
            {
                requestPath = "/Default.cshtml";
            }

            var fileInfo = _env.WebRootFileProvider.GetFileInfo(requestPath);



            if (fileInfo.Exists && CanHandle(fileInfo))
            {
                context.Response.StatusCode = 200;
                context.Response.ContentType = "text/html";

                var dynamicViewBag = new DynamicViewBag();
                dynamicViewBag.AddValue("IsHttps", context.Request.IsHttps);

                var config = new TemplateServiceConfiguration();
                // .. configure your instance
                config.TemplateManager = new ResolvePathTemplateManager(new[] { _env.MapPath("/Shared") });
                var service = RazorEngineService.Create(config);

                service.WithContext(new RazorPageContext { HttpContext = context });
                Engine.Razor = service;

                string result;
                if (Engine.Razor.IsTemplateCached(fileInfo.PhysicalPath, null))
                {
                    result = Engine.Razor.Run(fileInfo.PhysicalPath, null, null, dynamicViewBag);
                }
                else
                {
                    using (var stream = fileInfo.CreateReadStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var razor = await reader.ReadToEndAsync();
                            result = Engine.Razor.RunCompile(razor, fileInfo.PhysicalPath, null, null, dynamicViewBag);
                        }

                    }
                }

                await context.Response.WriteAsync(result);

                return;
            }

            await _next(context);
        }
        public async Task <string> CreatePageForIncidentAsync(string siteRootDirectory, string sectionId, Incident incident, IEnumerable <FileContent> inspectionPhotos, IEnumerable <Video> incidentVideos)
        {
            var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml");
            var template     = System.IO.File.ReadAllText(templateFile);
            var viewBag      = new RazorEngine.Templating.DynamicViewBag();

            viewBag.AddValue("InspectionPhotos", inspectionPhotos);
            viewBag.AddValue("IncidentVideos", incidentVideos);

            var html    = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag);
            var content = new MultipartFormDataContent();

            content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation");

            foreach (var image in inspectionPhotos)
            {
                var itemContent = new ByteArrayContent(image.Bytes);
                var contentType = MimeMapping.GetMimeMapping(image.Name);
                itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                content.Add(itemContent, image.Id);
            }

            this.pageEndPoint = string.Format("{0}/sections/{1}/pages", serviceBaseUrl, sectionId);
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint);

            requestMessage.Content = content;

            var responseMessage = await HttpSendAsync(requestMessage, accessToken);

            if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
            {
                throw new HttpResponseException(responseMessage.StatusCode);
            }

            var reponseObject = await GetReponseObjectAsync(responseMessage);

            return((string)reponseObject.links.oneNoteWebUrl.href);
        }
        public IHtmlString Build(IEnumerable<string> permissions)
        {
            var config = new TemplateServiceConfiguration();
            config.TemplateManager = new WatchingResolvePathTemplateManager(new[] { HttpContext.Current.Server.MapPath("~/Views/Shared") }, new InvalidatingCachingProvider());
            var service = RazorEngineService.Create(config);
            Engine.Razor = service;

            var viewBag = new DynamicViewBag();
            viewBag.AddValue("Permissions", permissions);

            string outputString = Engine.Razor.RunCompile("AdminMenu.cshtml", null,
                Contained.Where(
                    x => permissions.Contains(x.Permission) ||
                         permissions.Contains("*") ||
                         string.IsNullOrEmpty(x.Permission)), viewBag);

            return new HtmlString(outputString);
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="filePath"></param>
        /// <param name="encoding"></param>
        /// <param name="templateKey"></param>
        /// <param name="formatter"></param>
        private void WriteDocument <T>(T model, string filePath, Encoding?encoding, ITemplateKey templateKey, IClassDocumentFormatter formatter)
        {
            string dir = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            RazorEngine.Templating.DynamicViewBag viewBag = new RazorEngine.Templating.DynamicViewBag();
            viewBag.AddValue("Formatter", formatter);

            using StreamWriter writer = new StreamWriter(filePath, false, encoding ?? Encoding.UTF8);

            Engine.Run(templateKey, writer, typeof(T), model, viewBag: viewBag);

            writer.Flush();
        }
        public override Task Invoke(IOwinContext context)
        {
            var path = FileSystem.MapPath(_options.BasePath, context.Request.Path.Value);
            var ext = Path.GetExtension(path);
            if (CanHandle(ext))
            {
                Trace.WriteLine("Invoke RazorMiddleware");

                context.Response.StatusCode = 200;
                context.Response.ContentType = MimeTypeService.GetMimeType(".html");

                var dynamicViewBag = new DynamicViewBag();
                dynamicViewBag.AddValue("Options", _options);

                var razor = File.ReadAllText(path);
                var result = Razor.Parse(razor, null, dynamicViewBag, String.Empty);

                context.Response.Write(result);

                return Globals.CompletedTask;
            }

            return Next.Invoke(context);
        }
Example #12
0
        /// <inheritdoc/>
        public void WriteTypeDocument(TypeWithComment type, IClassDocumentFormatter formatter)
        {
            string filePath = GetTypeDocumentFilePath(type, false);
            string dir      = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            RazorEngine.Templating.DynamicViewBag viewBag = new RazorEngine.Templating.DynamicViewBag();
            viewBag.AddValue("Formatter", formatter);

            if (!TypeDocumentTemplateState.IsCompiled)
            {
                TypeDocumentTemplateState.Compile(typeof(TypeWithComment));
            }

            using (StreamWriter writer = new StreamWriter(filePath, false, Settings.TypeDodumentSettings.Encoding))
            {
                Engine.Run(TypeDocumentTemplateState.Key, writer, type.GetType(), type, viewBag: viewBag);
                writer.Flush();
            }
        }
        public async static Task<string> CreatePageForIncidentAsync(GraphServiceClient graphService, string siteRootDirectory, Group group, Section section, Incident incident, IEnumerable<FileContent> inspectionPhotos, IEnumerable<Models.Video> incidentVideos)
        {
            var accessToken = AuthenticationHelper.GetGraphAccessTokenAsync();
            var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml");
            var template = System.IO.File.ReadAllText(templateFile);
            var viewBag = new RazorEngine.Templating.DynamicViewBag();
            viewBag.AddValue("InspectionPhotos", inspectionPhotos);
            viewBag.AddValue("IncidentVideos", incidentVideos);

            var html = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag);
            var content = new MultipartFormDataContent();
            content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation");

            foreach (var image in inspectionPhotos)
            {
                var itemContent = new ByteArrayContent(image.Bytes);
                var contentType = MimeMapping.GetMimeMapping(image.Name);
                itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                content.Add(itemContent, image.Id);
            }

            var pageEndPoint = string.Format("{0}groups/{1}/notes/sections/{2}/pages", AADAppSettings.GraphBetaResourceUrl, group.Id, section.id);
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint);
            requestMessage.Content = content;

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await accessToken);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var responseMessage = await client.SendAsync(requestMessage);

                if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
                    throw new HttpResponseException(responseMessage.StatusCode);

                var payload = await responseMessage.Content.ReadAsStringAsync();

                return JObject.Parse(payload)["links"]["oneNoteWebUrl"]["href"].ToString();
            }
        }
Example #14
0
        public string SendEmail()
        {
            DateTime    _endDate = DateTime.Today.Date.AddDays(-35);
            MailAddress _From    = new MailAddress("*****@*****.**");
            MailAddress _To      = new MailAddress("*****@*****.**");
            //MailAddress _From = new MailAddress("*****@*****.**");
            //MailAddress _To = new MailAddress("*****@*****.**");
            MailMessage _Mail = new MailMessage(_From, _To);

            _Mail.To.Add("*****@*****.**");
            _Mail.To.Add("*****@*****.**");
            _Mail.IsBodyHtml = true;
            _Mail.Subject    = "DailyPerformance Report";
            string _html = "";
            string path  = HttpContext.Server.MapPath("~/Views/DailyQuotes/SendEmail.cshtml");

            var template = System.IO.File.ReadAllText(path);

            RazorEngine.Templating.DynamicViewBag _ViewBag = new RazorEngine.Templating.DynamicViewBag();
            _ViewBag.AddValue("test", "test data");

            List <DailyQuote> result = new List <DailyQuote>();

            GetCTQ();

            _ViewBag.AddValue("_CTQDatestring", _Dstring);
            _ViewBag.AddValue("_CTQ", _CTopQuote);
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Confused" && x.QuoteType == "Partner Quotes").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_PTQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Confused" && x.QuoteType == "Top Quote").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTopQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Confused" && x.QuoteType == "Top Quotes Q %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTopQP", GetData(result, true));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Confused" && x.QuoteType == "Top Quotes HI %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTopHIP", GetData(result, true));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Confused" && x.QuoteType == "Quotes Blocked %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CQBP", GetData(result, true));


            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go Compare" && x.QuoteType == "Total Quotes").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GTQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go Compare" && x.QuoteType == "Partner Quotes").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GPQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go Compare" && x.QuoteType == "Top Quote").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GTopQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go Compare" && x.QuoteType == "Top Quotes Q %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GTopQP", GetData(result, true));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go ComPare" && x.QuoteType == "Top Quotes HI %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GTopHIP", GetData(result, true));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Go Compare" && x.QuoteType == "Quotes Blocked %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_GQBP", GetData(result, true));



            //compare the Market
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Consumer Enquiries").OrderByDescending(x => x.Date).ToList();

            _ViewBag.AddValue("_CTMCE", GetData(result, false));
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Prices Presented").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTMPP", GetData(result, false));
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Prices Presented @P1").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTMPP1", GetData(result, false));
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Click Through (PCTs)").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTMCT", GetData(result, false));
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Top Quotes %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTMTQP", GetData(result, true));
            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Compare The Market" && x.QuoteType == "Quotes Declined %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_CTMQD", GetData(result, true));



            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Total Queries").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMTQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Total Quotes Returned").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMTQR", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Total Clicks").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMTC", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Provider Quote Rate %").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMPQR", GetData(result, true));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Filtered Quotes").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMFQ", GetData(result, false));

            result = db.DailyQuotes.Where(x => x.Date <= DateTime.Today.Date && x.Date >= _endDate && x.QuoteSource == "Money Supermarket" && x.QuoteType == "Top Quotes").OrderByDescending(x => x.Date).ToList();
            _ViewBag.AddValue("_MSMTQS", GetData(result, false));

            DailyQuote model = new DailyQuote();

            var config = new TemplateServiceConfiguration
            {
                Language             = Language.CSharp,
                EncodedStringFactory = new RawStringFactory()
            };

            config.EncodedStringFactory = new HtmlEncodedStringFactory();

            var service = RazorEngineService.Create(config);

            Engine.Razor = service;

            // _html= Razor.Parse(template,model,_ViewBag,null);
            var templateService = new TemplateService(config);

            _html = Engine.Razor.RunCompile(template, "Email", null, null, _ViewBag);



            _Mail.Body = _html;
            SmtpClient smtp = new SmtpClient
            {
                Port        = 25,
                Host        = "auth.smtp.1and1.co.uk",
                Credentials = new NetworkCredential("*****@*****.**", "paragon")
            };

            smtp.Send(_Mail);

            return("<h2>Email Sent</h2>");
        }