如何在 .net 中转换 Web 表单中的控制台应用程序?

How to convert console application in web forms in .net?

我有两个 classes,我用它们来获取 google 分析。我有控制台应用程序的代码。我想在网络表单中编写该代码。如何在网络表单中添加此代码?

如何在网页表单中添加这个完整的代码?

我添加了 .cs class 并编写了下面的代码

namespace GoogleAnalyticsAPI_Sample
{
        class GoogleConnector
        {
            public AnalyticsService Service { get; set; }

            public GoogleConnector(string keyPath, string accountEmailAddress)
            {
                var certificate = new X509Certificate2(keyPath, "notasecret", X509KeyStorageFlags.Exportable);

                var credentials = new ServiceAccountCredential(
                   new ServiceAccountCredential.Initializer(accountEmailAddress)
                   {
                       Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
                   }.FromCertificate(certificate));

                Service = new AnalyticsService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credentials,
                    ApplicationName = "NewDemoProject"
                });
            }

            public AnalyticDataPoint GetAnalyticsData(string profileId, string[] metrics, DateTime startDate, DateTime endDate)
            {
                // My Profile ID is : 98196912
                AnalyticDataPoint data = new AnalyticDataPoint();
                if (!profileId.Contains("ga:"))
                    profileId = string.Format("ga:{0}", profileId);

                GaData response = null;
                do
                {
                    int startIndex = 1;
                    if (response != null && !string.IsNullOrEmpty(response.NextLink))
                    {
                        Uri uri = new Uri(response.NextLink);
                        var paramerters = uri.Query.Split('&');
                        string s = paramerters.First(i => i.Contains("start-index")).Split('=')[1];
                        startIndex = int.Parse(s);
                    }

                    var request = BuildAnalyticRequest(profileId, metrics, startDate, endDate, startIndex);
                    response = request.Execute();
                    data.ColumnHeaders = response.ColumnHeaders;
                    data.Rows.AddRange(response.Rows);

                }
                while (!string.IsNullOrEmpty(response.NextLink));

                return data;
            }

            private DataResource.GaResource.GetRequest BuildAnalyticRequest(string profileId, string[] metrics,
                                                                                DateTime startDate, DateTime endDate, int startIndex)
            {
                DataResource.GaResource.GetRequest request = Service.Data.Ga.Get(profileId, startDate.ToString("yyyy-MM-dd"),
                                                                                    endDate.ToString("yyyy-MM-dd"), string.Join(",", metrics));
                request.StartIndex = startIndex;
                return request;
            }

            public class AnalyticDataPoint
            {
                public AnalyticDataPoint()
                {
                    Rows = new List<IList<string>>();
                }

                public IList<GaData.ColumnHeadersData> ColumnHeaders { get; set; }
                public List<IList<string>> Rows { get; set; }
            }
        }
}

在另一个 class 中,我称其为 class

namespace GoogleAnalyticsAPI_Sample
{
    class StartPoint
    {
        static void Main(string[] args)
        {
            GoogleConnector ga = new GoogleConnector(@"C:\Users\Sun\Desktop\infibeam\NewDemoProject-199399fasfaa.p12", "554818524279-g0erg68fvle5unijrup71efsdfasfd@developer.gserviceaccount.com");

            var nuberOfPageViews = ga.GetAnalyticsData("ga:9819111", new string[] { "ga:pageviews" },
                DateTime.Now.AddDays(-100), DateTime.Now).Rows[0][0];


            Console.WriteLine("page views : {0}\n Session Duration : {1} \n Avg Session Duration : {2}\n Bounce Rate :{3}\n hits: {4}\n Sessions:{5} ", nuberOfPageViews, sessionDuration, avgSessionDuration, bounceRate, sessions, bounces, hits, sessions);
            Console.Read();

        }
    }
}

你可以启动一个新的项目作为.dll库,然后在控制台设置你使用的代码并编译,你会得到一个.dll

然后打开 Web 表单项目并添加对该 .dll 的引用

P.D:记得在你的 .dll 库中将 类 设置为 public 以授予访问权限。