程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> ASP.NET簡單調度類(單入口)實現代碼

ASP.NET簡單調度類(單入口)實現代碼

編輯:關於C#
 

1. SimpleAttemper.cs
 

/// 簡單頁面調度類
public class SimpleAttemper : System.Web.UI.Page
{
private AttemperHelper_Simple ater;
private AtConfig_Simple atconf;

protected override void OnInit(EventArgs e)
{
atconf = new AtConfig_Simple();

if (atconf.Attemper_Name == "")
{
System.Web.HttpContext.Current.Response.Write("未定義表現層類");
System.Web.HttpContext.Current.Response.End();
}

ater = new AttemperHelper_Simple(atconf.Attemper_Name);

string ControlName = FormHelper.GetString(atconf.Control_Name);
if (ControlName == null)
{
ControlName = atconf.Control_Default;
}
string ActionName = FormHelper.GetString(atconf.Action_Name);
if (ActionName == null)
{
ActionName = atconf.Action_Default;
}

ater.Execute(ControlName.ToLower(), ActionName.ToLower());
}
}

/// 簡單調度執行類
public class AttemperHelper_Simple
{
private string _Attemper_Assembly;
private AtConfig_Simple _AtConf = new AtConfig_Simple();

public AttemperHelper_Simple(string Attemper_Assembly)
{
_Attemper_Assembly = Attemper_Assembly;
}

public Assembly Attemper_Assembly
{
get
{
return Assembly.Load(_Attemper_Assembly);
}
}

public AtConfig_Simple AtConf
{
get
{
return _AtConf;
}
}
/// 獲取所有控制器及方法
public List GetAllControls()
{
List ctls = new List();
Type[] types = Attemper_Assembly.GetTypes();
foreach (Type t in types)
{
if (t.Name.StartsWith(AtConf.Control_Prefix))
{
AtModel_Simple atmo_ctl = new AtModel_Simple();
atmo_ctl.Control_Name = t.Name.Replace(AtConf.Control_Prefix, "").ToLower();
atmo_ctl.Control = t.FullName;
atmo_ctl.Action_Name = "";
atmo_ctl.Action = "";
ctls.Add(atmo_ctl);
foreach (MethodInfo mf in t.GetMethods())
{
if (mf.Name.StartsWith(AtConf.Action_Prefix))
{
AtModel_Simple atmo_act = new AtModel_Simple();
atmo_act.Control_Name = atmo_ctl.Control_Name;
atmo_act.Control = atmo_ctl.Control;
atmo_act.Action_Name = mf.Name.Replace(AtConf.Action_Prefix, "").ToLower();
atmo_act.Action = mf.Name;
ctls.Add(atmo_act);
}
}
}
}
return ctls;
}

/// 獲取某個控制器方法
public AtModel_Simple GetControl(string ControlName, string ActionName)
{
return GetAllControls().SingleOrDefault(at => (at.Control_Name == ControlName.ToLower()) && (at.Action_Name == ActionName.ToLower()));
}

/// 執行某個控制器方法
public void Execute(string ControlName, string ActionName)
{
AtModel_Simple atmo = GetControl(ControlName, ActionName);
if (atmo == null)
{
//Console.WriteLine("動作調度失敗");
System.Web.HttpContext.Current.Response.Write("動作調度失敗");
return;
}
Type ctl = Attemper_Assembly.GetType(atmo.Control, true);
Object obj = Attemper_Assembly.CreateInstance(atmo.Control);

ctl.InvokeMember(atmo.Action, BindingFlags.InvokeMethod, null, obj, null);
}

}
/// 默認調度配置
public class AtConfig_Simple
{
private string _Control_Name = "ctl";
private string _Control_Prrefix = "Control";
private string _Control_Default = "Default";

private string _Action_Name = "act";
private string _Action_Prefix = "Action";
private string _Action_Default = "Index";

private string _Attemper_Name = "";
/// 控制器名
public string Control_Name
{
set
{
_Control_Name = value;
}
get
{
return _Control_Name;
}
}
/// 控制器前綴
public string Control_Prefix
{
set
{
_Control_Prrefix = value;
}
get
{
return _Control_Prrefix;
}
}
/// 默認控制器
public string Control_Default
{
set
{
_Control_Default = value;
}
get
{
return _Control_Default;
}
}
/// 動作名
public string Action_Name
{
set
{
_Action_Name = value;
}
get
{
return _Action_Name;
}
}
/// 動作前綴
public string Action_Prefix
{
set
{
_Action_Prefix = value;
}
get
{
return _Action_Prefix;
}
}
/// 默認動作
public string Action_Default
{
set
{
_Action_Default = value;  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved