2014年5月29日 星期四

用virtualbox 架站連到外面

from http://ithelp.ithome.com.tw/question/10072318

1. 把 VirtualBox XP 的網路卡設定,設為橋接介面卡
2. 把原本機的區域網路連線,多加一個內網 IP,比如 192.168.100.1
3. 進入 VirtualBox XP,把區域網路連線 IP 設為 192.168.100.2,Gateway 設為 192.168.100.1




2014年5月28日 星期三

[C#] enum列舉型別

form: http://ithelp.ithome.com.tw/question/10074623


使用enum的好處,提高程式的可讀性
像本例 男生是Male,女生是Female,我們一看就知道
case (short)Sex.Male://值為1
MessageBox.Show("男生");
break;//離開swith
case (short)Sex.Female://值為0
MessageBox.Show("女生");
break;//離開swith

而如果是用數字表示,就會搞不清楚那個是男 那個是女
case 1://值為1
MessageBox.Show("男生");
break;//離開swith
case 0://值為0
MessageBox.Show("女生");
break;//離開swith

使用enum有以下幾點要注意:
enum不能於方法內宣告
資料型別必須是byte、sbyte、short、ushort、int、unit、long或ulong型別
預設的預料型別為int
若列舉項目沒有給定任何整數值,則預設第一個列舉值為0,第二個列舉值為1,
以此類推
float與double不可為enum的資料型別

以下為本例程式碼及註解
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace Enum_ex  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.             cboNum.Items.Add("0");//集合索引值0的值為0  
  17.             cboNum.Items.Add("1");//集合索引值1的值為1  
  18.             cboNum.SelectedIndex = 1;//設定 預設索引值為1  
  19.         }  
  20.         public enum Sex : short  
  21.         {  
  22.             Female = 0,//enum每行以,結尾  
  23.             Male = 1,  
  24.         };  
  25.         private void btnShow_Click(object sender, EventArgs e)  
  26.         {  
  27.             short Num = 0;  
  28.             if (cboNum.Text != "")  
  29.             {  
  30.                 Num = short.Parse(cboNum.Text);//將選取文字轉為short型別  
  31.             }  
  32.             switch (Num)  
  33.             {  
  34.                 case (short)Sex.Male://值為1  
  35.                     MessageBox.Show("男生");  
  36.                     break;//離開swith  
  37.                 case (short)Sex.Female://值為0  
  38.                     MessageBox.Show("女生");  
  39.                     break;//離開swith  
  40.                 default:  
  41.                     MessageBox.Show("數值不在範圍內");//非男非女 才會執行到這邊 下拉選單只有 0 1 可以選,所以這個基本上不可能發生  
  42.                     break;//離開swith  
  43.             }  
  44.         }  
  45.     }  
  46. }  









2014年5月17日 星期六

[MVC] Controller 和 Action




以下為controller提供的型別


型別簡介Controller Helper Methods
EmptyResult甚麼事都不做…
ContentResult回傳文字至瀏覽器,可以自行設定content-type。Content
FileResult回傳二位元資料,例如Server上的mp3等檔案。File
ViewResult呈現指定或是預設的View。View
PartialViewResult呈現指定或是預設的View樣板PartialView
RedirectToRouteResult如果產生HTTP 301、302則會重新導向到另外一個Action或是其他指定的URL、或是設定好的Router。RedirectToAction
RedirectToActionPermanent
RedirectToRoute
RedirectToRoutePermanent
RedirectResult如果產生HTTP 301、302則會導向至另外一個URL。Redirect
RedirectPermanent
JsonResult將.net 物件序列化成Json格式並回傳。Json
JavaScriptResult回傳一個JavaScript的程式碼片段,通常用於Ajax裡面。JavaScript
HttpUnauthorizedResult回傳HTTP 401 ( 未授權 )。None
HttpNotFoundResult回傳HTTP 404。HttpNotFound
HttpStatusCodeResult返回指定的HTTP代碼。



public ViewResult DemoViewResult()
{
return View(); // Views/Home/DemoActionResult.cshtml
}
public JsonResult Ajax()
{
return Json(new { Title = "AsFlexer", Url = "http://asflexer.com" });
}
public ActionResult DemoActionResult()
{
return View();
return Redirect("http://asflexer.com"); //轉址到別的網站
return RedirectToAction("DemoActionResult"); //連結轉到另一個Action
return HttpNotFound(); //
}






[.NET] Attributes特性筆記

Attributes特性
[AcceptVerbs(…)]
To specify HTTP verbs an action method will respond to.
要指定HTTP動詞的將響應的一個操作方法。
[ActionName(…)]
To define the name of an action
定義一個動作的名稱
[AdditionalMetadata(…)]
to populate the ModelMetadata.AdditionalValues​​ dictionary for a model property.
填充一個模型屬性的ModelMetadata.AdditionalValues​​字典。
[AllowHtml]
To allow HTML markup during model binding by skipping request validation for the property.
允許HTML標記在模型綁定通過跳過屬性的請求驗證。
[AsyncTimeout(…)]
To set the timeout value, in milliseconds, for an asynchronous method.
要設置超時值,以毫秒為單位的異步方法。
[Authorize(…)]
To restrict access by callers to an action method.
調用者的操作方法來限制訪問。
[ChildActionOnly]
To indicate that an action method should be called only as a child action.
要表明,作為一個的操作方法只應被子動作調用。
[HandleError(...)]
to handle an exception that is thrown by an action method.
處理異常就是將一個操作方法拋出。
[HiddenInput(…)]
To render a property or field value as a hidden input element.
為了呈現一個隱藏的輸入元素屬性或字段的值。
[HttpDelete]
to restrict an action method to handle only HTTP DELETE requests.
限制一個動作方法只處理HTTP DELETE請求。
[HttpGet]
to restrict an action method to handle only HTTP Get requests.
限制一個動作方法只處理HTTP GET請求。
[HttpPost]
to restrict an action method to handle only HTTP Post requests.
限制一個動作方法只處理HTTP POST請求。
[HttpPut]
to restrict an action method to handle only HTTP Put requests.
限制一個動作方法只處理HTTP PUT請求。
[ModelBinder(..)]
to associate a model type to a model-builder type.
關聯模型類型的模型生成器類型。
[NoAsyncTimeout]
To set the timeout to infinite (never).
要設置超時無限(從不)。
[NonAction]
To indicate that a controller method is not an action method.
為了表明控制器方法不是一個操作方法。
[OutputCache(..)]
To mark an action method whose output will be cached.
為了標記其產出將是緩存的一個操作方法。
[Remote(…)]
It is used to invoke the server-based validation from client side.
它是用於從客戶端調用基於服務器的驗證。
[RequireHttps]
It forces an unsecured HTTP request to be re-sent over HTTPS.
它迫使重新通過HTTPS發送不安全的HTTP請求。
[SessionState(…)]
Specifies the session state of the controller.
指定控制器的會話狀態。
[ValidateInput(...)]
To mark action methods whose input must be validated.
為了標記一個動作方法的輸入必須進行驗證。





sample


using System;
namespace AttTargsCS
{

// 該Attribute只對類有效.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute
{
}


// 該Attribute只對方法有效.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute
{
}


// 該Attribute只對構造器有效。
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute
{
}


// 該Attribute只對字段有效.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute
{
}


// 該Attribute對類或者方法有效(組合).
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute
{
}


// 該Attribute對所有的元素有效.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute
{
}

//上面定義的Attribute施加到程序元素上的用法
[ClassTarget] //施加到類
[ClassMethodTarget]//施加到類
[AllTargets] //施加到類
public class TestClassAttribute
{
[ConstructorTarget] //施加到構造器
[AllTargets] //施加到構造器
TestClassAttribute()
{
}

[MethodTarget] //施加到方法
[ClassMethodTarget] //施加到方法
[AllTargets] //施加到方法
public void Method1()
{
}
 
[FieldTarget] //施加到字段
[AllTargets] //施加到字段
public int myInt;

static void Main(string[] args)
{
}
}
}








2014年5月11日 星期日

[c#] =>是甚麼鬼東西啦!!!! ( Lambda 運算式 )


From: http://www.dotblogs.com.tw/smartleos/archive/2013/01/08/86999.aspx




其實就是把 delegate 關鍵字省略成 「=>」運算子而已。

  //匿名委派的寫法
25  白宮.整理書房(delegate(Master 主人, Location 地點) {
26                        return 主人.Name + " 的 " + 地點.Name + "書房整理好了。";}
27                    );
28  //Lambda 運算式的寫法 2
29  白宮.整理書房((主人, 地點) => {
30                        return 主人.Name + " 的 " + 地點.Name + "書房整理好了。";}
31                    );


常見的有下面四種寫法:
1(int x, string s) => s.Length > x;  //明確指定傳入參數的型別,適用在無法型別推斷的時候。
2(a, b) => a + b;  //讓編譯器使用型別推斷省去撰寫傳入參數型別的寫法。
3a => a * a; //只有一個傳入參數時,可以省略圓括號。
4() => "L" "I" "N" "Q"//沒有傳入參數時,必須用空的圓括號。



在 LINQ 中,大多方法都提供 Func 的傳入參數,也就是都可以透過匿名委派傳入自定義的邏輯,例如:從一個數列中取奇數
1int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
2int oddNumbers = numbers.Count(n => n % 2 == 1);





這篇的觀念很詳細
 http://www.dotblogs.com.tw/hatelove/archive/2012/06/07/csharp-linq-lambda-introduction.aspx


01//第1種例子
02(x, y) => x == y
03 
04//第2種例子
05x => x + 1
06 
07//第3種例子
08() => DoSomething()
09 
10//第4種例子
11x => { x++; Console.WriteLine(x); return x; }














 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template