C#查询字符串生成器,在查询中已经使用Google UTM,也使用Umbraco,umbracoUrlAlias

问题描述 投票:0回答:1

我有一些使用Umbraco(可能只是一般的C#问题)的示例,它使用了“ umbracoUrlAlias”,这是同一页面的一个奇怪的快捷方式URL。无论如何,我要做的是获取“ umbracoUrlAlias”并重定向到实际页面的URL,然后通过查询字符串将“ umbracoUrlAlias”附加到基本页面URL的末尾以进行跟踪。

但是,我要让我们的内容作者开始将Google UTM用于外部广告系列的链接。这就带来了一个问题,因为如果我重定向到的URL已经有一个查询字符串(?alturl = Foo),我现在需要如果已经有一个查询字符串,然后追加UTM查询字符串(输入时未知)到我网址的末尾。示例:

备用网址是这个:

www.example.com/Foo

并且我将其重定向到以下页面的实际页面:

www.example.com/this-is-my-base-page?alturl=Foo

但是UTM页面将以:

www.example.com/Foo?utm_source=Bar&utm_medium=Char&utm_campaign=Eor

但是,我希望通过将UTM代码附加到已知URL的末尾来将URL重定向到URL:

www.example.com/this-is-my-base-page?alturl=Foo&utm_source=Bar&utm_medium=Char&utm_campaign=Eor

我当前的重定向代码是:

var currentUri = HttpContext.Current.Request.Url;
 var port = (currentUri.Port == 80) ? "" : ":" + currentUri.Port;
 var siteUrl = currentUri.Scheme + Uri.SchemeDelimiter + currentUri.Host;
  var canonicalUrl = Model.Content.Url;

  var theUrl = siteUrl + Model.Content.Url;
  var theRedirectUrl = "";
// use Umbraco Url alias as the canonical url if it is set
 if (Model.Content.HasValue("umbracoUrlAlias"))
{
    // umbracourlalias can be a comma delimited string of alterantive urls, the canonical url will be the first
    var canonicalUrls = Model.Content.GetPropertyValue<string>("umbracoUrlAlias").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    canonicalUrl = canonicalUrls.FirstOrDefault(); // this needs to take into account all redirects



    if (!String.IsNullOrEmpty(canonicalUrl))
    {
        if (!canonicalUrl.StartsWith("/"))
        {
            canonicalUrl = "/" + canonicalUrl;
        }
 //keep consistent with how your site Urls ending, eg add a slash if that is the convention you are implementing

    }

        foreach(var urlAlias in canonicalUrls){
        <input type="hidden" value="@urlAlias" />

          canonicalUrl = siteUrl + "/" + urlAlias;
          <input type="hidden" value="@canonicalUrl" />
             if(currentUri.ToString() == canonicalUrl){
               theRedirectUrl = theUrl;
               Response.Redirect(theRedirectUrl + "?vanityurl=" + urlAlias);
            }

        }
}

我的问题是,如上例所示,我将如何获取URL,然后返回URL,然后将UTM附加到末尾。

更新:我尝试了Dana的原始解决方案,该方法我认为可以解决,但是当URL类似于“ www.example.com/Foo?test=testingthis”时,重定向不会发生,但是如果没有查询,重定向部分就可以工作/ Foo之后的字符串。

c# redirect umbraco utm
1个回答
0
投票

如果您希望在重定向中包含以前的查询参数,请更改此行:

Response.Redirect(theRedirectUrl + "?vanityurl=" + urlAlias);

至以下内容:

var extraQuery = string.IsNullOrEmpty(currentUri.Query)
    ? ""
    : ("&" + currentUri.Query.TrimStart('?'));
Response.Redirect(theRedirectUrl + "?vanityurl=" + urlAlias + extraQuery);
© www.soinside.com 2019 - 2024. All rights reserved.