2015-05-04 76 views
-2

我想優化下面寫的代碼,如果你有答案請快速回復我,在這段代碼中我想提出不同的條件並在每個條件下返回不同的url如果有可能與優化的方式,然後回答我:我該如何優化asp.net的代碼

if (Offer1_Rb_Yes.Checked == true || DropDownList1.SelectedIndex > 0) 
    { 
     int offerid = MyOffers[0].OfferId; 
     DAL.offers Offer = new DAL.offers(); 
     Offer = obj.GetOffer(offerid); 
     if (Offer.CampId == "WINE-MAKERS-CHOICE") 
     { 
      url1 = url1 + "WINE-MAKERS-CHOICE&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Gender; 
     } 
     else if (Offer.CampId == "LETS-INSURE-CA") 
     { 
      url1 = url1 + "LETS-INSURE-CA&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Age; 
     } 
     else if (Offer.CampId == "SOLAR-BROKER") 
     { 
      url1 = url1 + "SOLAR-BROKER&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Age; 
     } 
     else if (Offer.CampId == "WENATEX-AU") 
     { 
      url1 = url1 + "WENATEX-AU&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&street1=" + street1 + "&towncity=" + suburb + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&source=" + Source; 
     } 
     else if (Offer.CampId == "MODERN-SOLAR-COMP") 
     { 
      url1 = url1 + "MODERN-SOLAR-COMP&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&street1=" + street1 + "&towncity=" + suburb + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&phone2=" + Phone2 + "&gender=" + Gender + "&solar_pv=" + Solar_pv; 
     } 
     else if (Offer.CampId == "CAREERS-AUSTRALIA-3") 
     { 
      url1 = url1 + "CAREERS-AUSTRALIA-3&sid=TPF34" + "&firstname=" + FirstName + "&lastname=" + LastName + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1; 
     } 
     else if (Offer.CampId == "GOOD-LIFE") 
     { 
      url1 = url1 + "GOOD-LIFE&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&postcode=" + PostCode + "&phone1=" + Phone1; 
     } 
     else if (Offer.CampId == "GRYPHON-SOLAR") 
     { 
      url1 = url1 + "GRYPHON-SOLAR&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&county=VIC&postcode=" + PostCode + "&phone1=" + Phone1 + "&source=" + Source + "&age=" + Age; 
     } 
     else if (Offer.CampId == "GRYPHON-SOLAR-WA") 
     { 
      url1 = url1 + "GRYPHON-SOLAR-WA&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&county=WA&postcode=" + PostCode + "&phone1=" + Phone1 + "&source=" + Source + "&age=" + Age; 
     } 
     else if (Offer.CampId == "ACQUIRE-LEARNING") 
     { 
      url1 = url1 + "ACQUIRE-LEARNING&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&industry=" + Industry; 
     } 
     else if (Offer.CampId == "KOGAN") 
     { 
      url1 = url1 + "KOGAN&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&towncity=" + suburb + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&ipaddress=" + ipaddress; 
     } 
    } 
+1

那麼你需要張貼這個問題** [這裏](HTTP://codereview.stackexchange .com /)** –

回答

0

我會把它下面寫:

if (offer5_Rb_Yes.Checked == true) 
{ 
    int offerid = MyOffers[4].OfferId; 
    DAL.offers Offer = new DAL.offers(); 
    Offer = obj.GetOffer(offerid); 
    url5=url5+Offer.CampId+"&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Age; 
    return url5; 
} 

我看到唯一獲取您的網址更改爲Offer.CampId和其他部分保持不變!所以你可以像上面那樣做!

UPDATE

我只注意到的是,在第一個條件你url有在年底小的變化!不知道是有意的還是錯誤的!如果是故意的,那麼你可以嘗試使用ternary operators爲您url只爲一個條件的變化,並保持相同的其他2

url5= Offer.CampId=="WINE-MAKERS-CHOICE"? 
     url5+Offer.CampId+"&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Gender 
     :url5+Offer.CampId+"&sid=TPF34" + "&email=" + Email + "&firstname=" + FirstName + "&lastname=" + LastName + "&dob=" + DOB + "&county=" + State + "&postcode=" + PostCode + "&phone1=" + Phone1 + "&c1=" + Age; 
+0

謝謝你的建議是非常好的,但我有11類型的網址和url的參數總是變化,在上面的代碼我只提到三種類型的網址 –

+0

發佈所有條件和URL .. lemme檢查.. –

+0

現在我編輯我的請建議我 –