예제 #1
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (Url == null)
            {
                return;
            }

            output.CopyHtmlAttribute(UrlAttributeName, context);

            Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext);
            UrlUtils.FromRelative(urlHelper.Content(Url), out PathString path, out QueryString query, out _);

            string url = null;
            var    n   = _bundleManagerFactory.Instances.Count;

            for (var i = 0; i < n; i++)
            {
                if ((url = await _bundleManagerFactory.Instances[i].TryGenerateUrlAsync(path, query, ViewContext.HttpContext)) != null)
                {
                    break;
                }
            }

            if (url != null)
            {
                var index = output.Attributes.IndexOfName(UrlAttributeName);
                TagHelperAttribute existingAttribute = output.Attributes[index];
                output.Attributes[index] = new TagHelperAttribute(existingAttribute.Name, url, existingAttribute.ValueStyle);
            }
        }
예제 #2
0
        public static bool IsRequestFromLocalHost(this HttpRequest httpReq, Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper)
        {
            var callingUrl = httpReq.Headers["Referer"].ToString();
            var isLocal    = urlHelper.IsLocalUrl(callingUrl);

            return(isLocal);
        }
예제 #3
0
        // https://stackoverflow.com/questions/40001242/aspnetcore-get-path-to-wwwroot-in-taghelper
        public override void Process(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context
                                     , Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput output)
        {
            EnsureFileVersionProvider();


            Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper =
                base.UrlHelperFactory.GetUrlHelper(this.ActionContext);

            // string myUrl = urlHelper.Content("~/somefilebelowwwwroot");



            // string imgPath = output.Attributes["src"].Value as string;
            // Microsoft.AspNetCore.Html.HtmlString hs = output.Attributes["src"].Value as Microsoft.AspNetCore.Html.HtmlString;
            // string imgPath = hs.Value;

            // Microsoft.AspNetCore.Html.HtmlString hs = context.AllAttributes["src"].Value as Microsoft.AspNetCore.Html.HtmlString;
            // string imgPath = hs.Value;

            // var rcc = new Microsoft.AspNetCore.Mvc.Routing.UrlRouteContext();
            // var rc = new Microsoft.AspNetCore.Routing.RouteContext(this.HttpContext);

            // lol = urlHelper.RouteUrl(rcc);



            string baseSrc = context.AllAttributes["src"].Value.ToString();

            if (baseSrc.StartsWith("data:"))
            {
                return;
            }

            PathMap pm = PathMap.FromUrl(this.HostingEnvironment, urlHelper, baseSrc);

            System.IO.FileInfo fi = new System.IO.FileInfo(pm.Physical);

            string unixTicks = fi.Exists ? fi.LastWriteTimeUtc.ToUnixTicksString()
                : System.DateTime.UtcNow.ToUnixTicksString();

            if (string.IsNullOrEmpty(pm.QueryString))
            {
                baseSrc = $"{pm.Absolute}?no_cache={unixTicks}{pm.Hash}";
            }
            else
            {
                baseSrc = $"{pm.Absolute}{pm.QueryString}&no_cache={unixTicks}{pm.Hash}";
            }

            // No not have it XML-Attribute-encoded
            //output.Attributes.SetAttribute("src", new Microsoft.AspNetCore.Html.HtmlString(imgPath));
            output.Attributes.SetAttribute("src", baseSrc);

            //output.Attributes.SetAttribute("src", src + "?&v=123");
            // output.Attributes.SetAttribute(SrcAttributeName, _fileVersionProvider.AddFileVersionToPath(Src));
        }
예제 #4
0
        public static string AliasUrl(this Microsoft.AspNetCore.Mvc.IUrlHelper helper, string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return("");
            }

            url = url.Trim();                //Removes Empty Spaces
            if (url.Length > 100)            //if its bigger than 100 characters
            {
                url = url.Substring(0, 100); //Take first 100 character
            }
            //Türkçe karakterden arındır
            url = url.Replace("İ", "I");
            url = url.Replace("ı", "i");
            url = url.Replace("Ğ", "G");
            url = url.Replace("ğ", "g");
            url = url.Replace("Ç", "C");
            url = url.Replace("ç", "c");
            url = url.Replace("Ö", "O");
            url = url.Replace("ö", "o");
            url = url.Replace("Ş", "S");
            url = url.Replace("ş", "s");
            url = url.Replace("Ü", "U");
            url = url.Replace("ü", "u");
            //Diğer işaretlerden arındır
            url = url.Replace("'", "");  //Example sal'am = salam
            url = url.Replace("\"", ""); //Example "salam" = salam

            char[] degistir = @"!'^+%&/()=?_*""£#$½{[]}\~.,:;<>|€@".ToCharArray();
            for (int i = 0; i < degistir.Length; i++)
            {
                if (url.Contains(degistir[i]))
                {
                    url = url.Replace(degistir[i].ToString(), string.Empty);
                }
            }
            //herhangi bir karakter a'dan z ye ve A'dan Z'ye 0'dan 9'a Altı çizili olanlar hariç
            Regex r = new Regex("[^a-zA-Z0-9_-]");

            url = r.Replace(url, "-");
            while (url.IndexOf("--") > -1)
            {
                url = url.Replace("--", "-");
            }
            return(url);
        }
예제 #5
0
        public static string UrlFormat(this Microsoft.AspNetCore.Mvc.IUrlHelper helper, string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return("");
            }
            url = url.ToLower();
            url = url.Trim();
            if (url.Length > 100)
            {
                url = url.Substring(0, 100);
            }
            url = url.Replace("İ", "I");
            url = url.Replace("ı", "i");
            url = url.Replace("ğ", "g");
            url = url.Replace("Ğ", "G");
            url = url.Replace("ç", "c");
            url = url.Replace("Ç", "C");
            url = url.Replace("ö", "o");
            url = url.Replace("Ö", "O");
            url = url.Replace("ş", "s");
            url = url.Replace("Ş", "S");
            url = url.Replace("ü", "u");
            url = url.Replace("Ü", "U");
            url = url.Replace("'", "");
            url = url.Replace("\"", "");
            char[] replacerList = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
            for (int i = 0; i < replacerList.Length; i++)
            {
                string strChr = replacerList[i].ToString();
                if (url.Contains(strChr))
                {
                    url = url.Replace(strChr, string.Empty);
                }
            }

            Regex r = new Regex("[^a-zA-Z0-9_-]");

            url = r.Replace(url, "-");
            while (url.IndexOf("--") > -1)
            {
                url = url.Replace("--", "-");
            }
            return(url);
        }
예제 #6
0
 public UrlHelper(Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper)
 {
     this.urlHelper = urlHelper;
 }
예제 #7
0
            public static PathMap FromUrl(Microsoft.AspNetCore.Hosting.IHostingEnvironment env
                                          , Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper
                                          , string baseSrc)
            {
                if (baseSrc.StartsWith("data:"))
                {
                    throw new System.ArgumentException("data: cannot be transformed into URL.");
                }

                PathMap pmr = new PathMap();

                pmr.Original = baseSrc;

                int argsPos = baseSrc.IndexOf("?");
                int hashPos = baseSrc.IndexOf("#");

                pmr.QueryString = "";
                pmr.Hash        = "";

                if (hashPos != -1)
                {
                    pmr.Hash = baseSrc.Substring(hashPos);
                    baseSrc  = baseSrc.Substring(0, hashPos);
                }

                if (argsPos != -1)
                {
                    pmr.QueryString = baseSrc.Substring(argsPos);
                    baseSrc         = baseSrc.Substring(0, argsPos);
                }

                string imageVirtualPath = null;


                if (
                    baseSrc.StartsWith("http:", System.StringComparison.InvariantCultureIgnoreCase) ||
                    baseSrc.StartsWith("https:", System.StringComparison.InvariantCultureIgnoreCase))
                {
                    pmr.Canonical = baseSrc;

                    string domain = urlHelper.ActionContext.HttpContext.Request.Scheme + "://"
                                    + urlHelper.ActionContext.HttpContext.Request.Host.Value;
                    if (baseSrc.StartsWith(domain, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        baseSrc = baseSrc.Substring(domain.Length);
                    }
                    else
                    {
                        pmr.PhysicalApplicable = false;

                        System.Uri uri = new System.Uri(baseSrc, System.UriKind.Absolute);
                        pmr.Absolute = uri.AbsolutePath;
                    }
                }

                if (baseSrc.StartsWith("~"))
                {
                    imageVirtualPath = urlHelper.Content(baseSrc); // To Virtual Path
                }
                else if (baseSrc.StartsWith("/"))
                {
                    imageVirtualPath = baseSrc;
                }
                else if (
                    !baseSrc.StartsWith("http:", System.StringComparison.InvariantCultureIgnoreCase) &&
                    !baseSrc.StartsWith("https:", System.StringComparison.InvariantCultureIgnoreCase)
                    )
                {
                    imageVirtualPath  = urlHelper.ActionContext.HttpContext.Request.PathBase.Value;
                    imageVirtualPath += urlHelper.ActionContext.HttpContext.Request.Path.Value;

                    if (!string.IsNullOrEmpty(imageVirtualPath))
                    {
                        if (!imageVirtualPath.EndsWith("/"))
                        {
                            int li = imageVirtualPath.LastIndexOf('/');
                            if (li != -1)
                            {
                                imageVirtualPath = imageVirtualPath.Substring(0, li + 1);
                            }
                            System.Console.WriteLine(imageVirtualPath);
                        }

                        //if (!string.IsNullOrEmpty(imageVirtualPath))
                        //        imageVirtualPath += "/";
                    }
                    imageVirtualPath += baseSrc;
                }

                pmr.Absolute = imageVirtualPath;

                if (pmr.Canonical == null)
                {
                    pmr.Canonical = urlHelper.ActionContext.HttpContext.Request.Scheme
                                    + "://"
                                    + urlHelper.ActionContext.HttpContext.Request.Host.Value;

                    pmr.Canonical += pmr.Absolute;

                    // System.Uri bas = new System.Uri(HttpContext.Request.Scheme + "://" + HttpContext.Request.Host.Value, System.UriKind.Absolute);
                    // System.Uri canonicalUrl = new System.Uri(bas, imagePhysicalPath);
                }

                if (pmr.PhysicalApplicable && env != null)
                {
                    string rootPath = urlHelper.Content("~"); // To Virtual Path

                    if (imageVirtualPath.StartsWith(rootPath, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        pmr.Physical = imageVirtualPath.Substring(rootPath.Length);
                    }
                    else
                    {
                        pmr.Physical = imageVirtualPath;
                    }

                    if (pmr.Physical.StartsWith('/'))
                    {
                        pmr.Physical = pmr.Physical.Substring(1);
                    }

                    // https://stackoverflow.com/questions/40001242/aspnetcore-get-path-to-wwwroot-in-taghelper
                    // this.HostingEnvironment.WebRootPath    //  d.h: /wwwroot
                    // this.HostingEnvironment.ContentRootPath // d.h:  /wwwroot/..
                    pmr.Physical = pmr.Physical.Replace('/', System.IO.Path.DirectorySeparatorChar);
                    pmr.Physical = System.IO.Path.Combine(env.WebRootPath, pmr.Physical);
                    pmr.Physical = System.IO.Path.GetFullPath(pmr.Physical);
                }

                return(pmr);
            }
예제 #8
0
 // Will not return physical path, only absolute + Canonical
 public static PathMap FromUrl(Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper, string baseSrc)
 {
     return(FromUrl(null, urlHelper, baseSrc));
 }