Ejemplo n.º 1
0
        /// <inheritdoc />
        public IHtmlContent AntiForgeryToken()
        {
            // If we're inside a BeginForm/BeginRouteForm, the antiforgery token might have already been
            // created and appended to the 'end form' content.
            if (ViewContext.FormContext.HasAntiforgeryToken)
            {
                return(HtmlString.Empty);
            }

            ViewContext.FormContext.HasAntiforgeryToken = true;
            var html = _htmlGenerator.GenerateAntiforgery(ViewContext);

            return(html ?? HtmlString.Empty);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步访问并呈现当前标签实例。
        /// </summary>
        /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param>
        /// <param name="output">当前标签输出实例,用于呈现标签相关信息。</param>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = null;
            AppendLibraries(output);
            var content = await output.GetChildContentAsync();

            if (!content.IsEmptyOrWhiteSpace)
            {
                output.AppendHtml(content.GetContent().Trim());
            }
            if (AttachStatusMessage)
            {
                var status = GetStatusMessage();
                if (status != null)
                {
                    output.AppendHtml("script", x =>
                    {
                        x.MergeAttribute("type", "text/javascript");
                        x.InnerHtml.AppendHtml("$(function(){");
                        x.InnerHtml.AppendHtml($"if(window.showMsg){{showMsg({status.ToJsonString()});}}else{{alert('{_scriptEncoder.Encode(status.Message)}');}}");
                        x.InnerHtml.AppendHtml("});");
                    });
                }
            }
            if (GenerateAntiforgery)
            {
                output.AppendHtml("form", builder =>
                {
                    builder.GenerateId("ajax-protected-form", "-");
                    builder.MergeAttribute("method", "post");
                    builder.MergeAttribute("action", HttpContext.Request.Path);
                    builder.InnerHtml.AppendHtml(_generator.GenerateAntiforgery(ViewContext));
                });
            }
        }
Ejemplo n.º 3
0
        public MvcForm LayuiBeginForm(string actionName, string controllerName, object routeValues, FormMethod method, bool?antiforgery, object htmlAttributes)
        {
            ViewContext.FormContext = new FormContext
            {
                CanRenderAtEndOfForm = true
            };

            var tagBuilder = _htmlGenerator.GenerateForm(ViewContext, actionName, controllerName, routeValues,
                                                         HtmlHelper.GetFormMethodString(method), htmlAttributes);

            if (tagBuilder != null)
            {
                tagBuilder.AddCssClass("layui-form");
                tagBuilder.MergeAttribute("auto-bind", "true", replaceExisting: false);
                tagBuilder.TagRenderMode = TagRenderMode.StartTag;
                tagBuilder.WriteTo(ViewContext.Writer, _htmlEncoder);
            }
            var shouldGenerateAntiforgery = antiforgery ?? method != FormMethod.Get;

            if (shouldGenerateAntiforgery)
            {
                ViewContext.FormContext.EndOfFormContent.Add(_htmlGenerator.GenerateAntiforgery(ViewContext));
            }

            return(new MvcForm(ViewContext, _htmlEncoder));
        }
Ejemplo n.º 4
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            TagBuilder tagBuilder = Generator.GenerateForm(ViewContext, ViewContext.RouteData.Values["action"].ToString(),
                                                           ViewContext.RouteData.Values["controller"].ToString(), null, null, null);

            output.MergeAttributes(tagBuilder);

            if (Antiforgery == true)
            {
                output.PostContent.AppendHtml(Generator.GenerateAntiforgery(ViewContext));
            }
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public IHtmlContent AntiForgeryToken()
        {
            var html = _htmlGenerator.GenerateAntiforgery(ViewContext);

            return(html ?? HtmlString.Empty);
        }
Ejemplo n.º 6
0
 public IHtmlContent GenerateAntiforgery(ViewContext viewContext)
 {
     return(_htmlGenerator.GenerateAntiforgery(viewContext));
 }