.NET将变量传递给第二个代码块

时间:2018-01-12 15:07:48

标签: c# asp.net .net

非常简单的问题虽然我很困惑,我发现自己在我的控制器中使用了这个功能,所以把它移到了我的第一个方法。 foreach循环有一个最终字符串FinalURL如何将其传递给httpWebRequest?我是否可以制作另一种方法并在休息后调用它?在我使用viewbag变量之前,但在此上下文中我无法使用它。如果我删除括号,它表示不是所有路径都返回一个函数,我相信这不是正确的方法。

namespace Linkofy.Models
{
public class MajesticFunctions
{
    public static int[] MajesticChecker(string[] URLtests)
    {
        foreach (string URLt in URLtests)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URLt);
            myHttpWebRequest.AllowAutoRedirect = false;
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            int resulting = (int)myHttpWebResponse.StatusCode;
            if (resulting == 200)
            {
                String Urlnew = URLt;
                String FinalURL = URLt.Replace("https://", "").Replace("http://", "");
                break;
            }
        }
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.majestic.com/api/json?app_api_key=KEY&cmd=GetIndexItemInfo&items=1&item0=" + FinalURL + "&datasource=fresh");
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                JObject jObject = JObject.Parse(reader.ReadToEnd());
                JToken Trusty = jObject["DataTables"]["Results"]["Data"][0]["TrustFlow"].Value<int>();
                JToken City = jObject["DataTables"]["Results"]["Data"][0]["CitationFlow"].Value<int>();
                JToken RIPy = jObject["DataTables"]["Results"]["Data"][0]["RefIPs"].Value<int>();

                int Trustflow = Int32.Parse(Trusty.ToString());
                int Citationflow = Int32.Parse(City.ToString());
                int Reffering = Int32.Parse(RIPy.ToString());

                int[] Metrics = new int[] { Trustflow, Citationflow, Reffering };

                return Metrics;
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

把它放在循环的外面。

这样它就可以在循环之外的范围内使用。

$terms = get_terms( 'post_tag', array(
'hide_empty' => false,
) );

$terms = get_terms( 'category', array(
'hide_empty' => false,
) );

答案 1 :(得分:0)

您还可以在for循环中移动WebRequest代码

namespace Linkofy.Models
{
public class MajesticFunctions
{
    public static int[] MajesticChecker(string[] URLtests)
    {
        foreach (string URLt in URLtests)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URLt);
            myHttpWebRequest.AllowAutoRedirect = false;
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            int resulting = (int)myHttpWebResponse.StatusCode;
            if (resulting == 200)
            {

                String FinalURL = URLt.Replace("https://", "").Replace("http://", "");
                if (FinalURL.Length > 0)
                {
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.majestic.com/api/json?app_api_key=KEY&cmd=GetIndexItemInfo&items=1&item0=" + FinalURL + "&datasource=fresh");
                  {
                     WebResponse response = request.GetResponse();
                     using (Stream responseStream = response.GetResponseStream())
                     {
                         StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                         JObject jObject = JObject.Parse(reader.ReadToEnd());
                         JToken Trusty = jObject["DataTables"]["Results"]["Data"][0]["TrustFlow"].Value<int>();
                         JToken City = jObject["DataTables"]["Results"]["Data"][0]["CitationFlow"].Value<int>();
                         JToken RIPy = jObject["DataTables"]["Results"]["Data"][0]["RefIPs"].Value<int>();

                         int Trustflow = Int32.Parse(Trusty.ToString());
                         int Citationflow = Int32.Parse(City.ToString());
                         int Reffering = Int32.Parse(RIPy.ToString());

                         int[] Metrics = new int[] { Trustflow, Citationflow, Reffering };

                         return Metrics;
                      }
                  }
                }
                break;
            }
        }
        throw new Exception("No Final URL Found");

       }
}
}

抱歉格式不正确