【问题标题】:How to use Google Adsense with MVC C# Razor如何在 MVC C# Razor 中使用 Google Adsense
【发布时间】:2013-05-31 09:32:30
【问题描述】:

我正在尝试将 Google Adsense 添加到我的 MVC 移动网络应用程序中。我想实施 Google Adsense 移动内容广告代码。他们拥有的脚本语言是asp 3.0。是否有可用于在 MVC C# Razor 页面上显示这些广告的 MVC 助手?我根本找不到任何关于 MVC 和在线显示网络广告的信息。希望得到一些帮助,我完全被困住了。

如果我使用常规的 Adsense Javascript 代码。当我在站点周围单击时,代码不会加载。只有当我点击页面上的刷新时。

【问题讨论】:

    标签: c# asp.net-mvc razor adsense


    【解决方案1】:

    从 Adsense ASP 示例翻译的简单 HtmlHelper:

    public static class AdsenseHelper
    {
        public static MvcHtmlString Adsense(this HtmlHelper htmlHelper, string clientKey, string adSlot)
        {
            var context = htmlHelper.ViewContext.HttpContext;
            var request = context.Request;
    
            int googleTime = (DateTime.Now - new DateTime(1970, 1, 1)).Days;
            var googleDt = (1000 * googleTime) + Math.Round(1000d * (DateTime.Now - DateTime.Today).Milliseconds);
    
            var googleUserAgent = context.Server.UrlEncode(request.ServerVariables["HTTP_USER_AGENT"]);
            var googleScheme = (string.Compare(request.ServerVariables["HTTPS"], "on") == 0) ? "https://" : "http://";
    
            var googleAdUrl =
                "http://pagead2.googlesyndication.com/pagead/ads?" +
                "client=" + clientKey + // ca-mb-pub-0000000000000000
                "&dt=" + googleDt +
                "&ip=" + context.Server.UrlEncode(request.ServerVariables["REMOTE_ADDR"]) +
                "&markup=xhtml" +
                "&output=xhtml" +
                "&ref=" + context.Server.UrlEncode(request.ServerVariables["HTTP_REFERER"]) +
                "&slotname=" + adSlot + // 0000000000
                "&url=" + context.Server.UrlEncode(googleScheme + request.ServerVariables["HTTP_HOST"] + request.ServerVariables["URL"]) +
                "&useragent=" + googleUserAgent +
                GoogleScreenRes(context.Request) +
                GoogleMuid(context.Request) +
                GoogleViaAndAccept(context, googleUserAgent);
    
            using (var client = new System.Net.WebClient())
            {
                string result = client.DownloadString(googleAdUrl);
                return new MvcHtmlString(result);
            }
        }
    
        private static string GoogleColor(string value, int random)
        {
            var colorArray = value.Split(',');
            return colorArray[random % (colorArray.Length)];
        }
    
        private static string GoogleScreenRes(HttpRequestBase request)
        {
            var screenRes = request.ServerVariables["HTTP_UA_PIXELS"];
            char delimiter = 'x';
            if (string.IsNullOrEmpty(screenRes))
            {
                screenRes = request.ServerVariables["HTTP_X_UP_DEVCAP_SCREENPIXELS"];
                delimiter = ',';
            }
            if (string.IsNullOrEmpty(screenRes))
            {
                screenRes = request.ServerVariables["HTTP_X_JPHONE_DISPLAY"];
                delimiter = '*';
            }
    
            if (screenRes != null)
            {
                string[] resArray = screenRes.Split(new[] { delimiter }, 2);
                if (resArray.Length == 2)
                {
                    return "&u_w=" + resArray[0] + "&u_h=" + resArray[1];
                }
            }
            return string.Empty;
        }
    
        private static string GoogleMuid(HttpRequestBase request)
        {
            var muid = request.ServerVariables["HTTP_X_DCMGUID"];
            if (!string.IsNullOrEmpty(muid))
            {
                return "&muid=" + muid;
            }
            muid = request.ServerVariables["HTTP_X_UP_SUBNO"];
            if (!string.IsNullOrEmpty(muid))
            {
                return "&muid=" + muid;
            }
            muid = request.ServerVariables["HTTP_X_JPHONE_UID"];
            if (!string.IsNullOrEmpty(muid))
            {
                return "&muid=" + muid;
            }
            muid = request.ServerVariables["HTTP_X_EM_UID"];
            if (!string.IsNullOrEmpty(muid))
            {
                return "&muid=" + muid;
            }
            return string.Empty;
        }
    
        private static string GoogleViaAndAccept(HttpContextBase context, string googleUserAgent)
        {
            if (string.IsNullOrEmpty(googleUserAgent))
                return string.Empty;
    
            string googleViaAndAccept = string.Empty;
            var via = context.Server.UrlEncode(context.Request.ServerVariables["HTTP_VIA"]);
            if (!string.IsNullOrEmpty(via))
            {
                googleViaAndAccept = "&via=" + via;
            }
            var accept = context.Server.UrlEncode(context.Request.ServerVariables["HTTP_ACCEPT"]);
            if (!string.IsNullOrEmpty(accept))
            {
                googleViaAndAccept = googleViaAndAccept + "&accept=" + accept;
            }
    
            return googleViaAndAccept;
        }
    }
    

    此帮助程序不适用于桌面浏览器。但可以通过将用户代理更改为 Opera Mini 来绕过。

    就像谷歌说的:

    AdSense 移动内容广告仅适用于针对旧功能手机设计的网站。随着完全支持网络的智能手机变得越来越普遍,我们建议您使用 AdSense 内容广告来满足您的所有内容广告需求。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2014-10-15
      • 2018-02-09
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多