
2009年8月17日
action:
public ActionResult Register()
{
var items = new List<SelectListItem>()
{
(new SelectListItem() {Text = "保密", Value = "0", Selected = true}),
(new SelectListItem() {Text = "男", Value = "1", Selected = false}),
(new SelectListItem() {Text = "女", Value = "2", Selected = false})
};
ViewData["items"] = items;
return View();
}
view:
<label for="sex">性别:</label>
<%=Html.DropDownList("items")%>
posted @ 2009-08-17 22:08 大陆响尾蛇 阅读(162) 评论(0)
编辑

2009年8月12日
自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的application path相结合进行扩展,任何问题都迎刃而解。如果你需要在所处的域或者子域处理数据标记的话,强制使用Default。
遗憾的是,ASP.NET MVC是基于虚拟目录的,在实际项目却有各种各样的需求方案。
例如:
1:应用程序是多语言的,像cn.example.com应该被匹配到“www.{language}example.com”路由上。
2:应用程序是多用户的,像username.example.com应该被匹配到“www.{clientname}.example.com”路由上。
3:应用程序是多子域的,像mobile.example.com应该被匹配到"www.{controller}.example.com/{action}....” 。
坐下来,深呼吸,开始我们ASP.NET MVC的神奇之旅吧。
定义routes
下面是我们定义简单的route,不带任何controller控制的route:

Code
routes.Add("DomainRoute", new DomainRoute(
"home.example.com", // Domain with parameters
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
另一个例子是用我们的controller控制域名:

Code
routes.Add("DomainRoute", new DomainRoute(
"{controller}.example.com", // Domain with parameters< br /> "{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
打算用controller 和action完全控制域名?

Code
routes.Add("DomainRoute", new DomainRoute(
"{controller}-{action}.example.com", // Domain with parameters
"{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
接下来是多语言route:

Code
routes.Add("DomainRoute", new DomainRoute(
"{language}.example.com", // Domain with parameters
"{controller}/{action}/{id}", // URL with parameters
new { language = "en", controller = "Home", action = "Index", id = "" } // Parameter defaults
));
HtmlHelper 扩展方法
因为我们不希望所有的URL所产生HtmlHelper ActionLink要使用full URLs,第一件事我们会添加一些新的ActionLink,其中载有boolean flag是否要full URLs或没有。利用这些,现在您可以添加一个链接到一个Action如下:
<%= Html.ActionLink("About", "About", "Home", true)%>
跟你以往的习惯没有什么不同,不是吗?
以下是一小段代码:

Code
public static class LinkExtensions
{
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, bool requireAbsoluteUrl)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary(), requireAbsoluteUrl);
}
// more of these
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool requireAbsoluteUrl)
{
if (requireAbsoluteUrl)
{
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);
routeData.Values["controller"] = controllerName;
routeData.Values["action"] = actionName;
DomainRoute domainRoute = routeData.Route as DomainRoute;
if (domainRoute != null)
{
DomainData domainData = domainRoute.GetDomainData(new RequestContext(currentContext, routeData), routeData.Values);
return htmlHelper.ActionLink(linkText, actionName, controllerName, domainData.Protocol, domainData.HostName, domainData.Fragment, routeData.Values, null);
}
}
return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}
}
在这没什么特别的:有许多的扩展方法,把扩展的URL加到域名上。这是一个预设ActionLink helpers,我的精神食粮来了DomainRoute class(详见:Dark Magic)
Dark magic
瞥眼之间,您可能已经看到了我的DomainRoute类代码段。这个类实际上是提取子域,并增加了象征性支持域部分的传入的URL,
我们将扩展基类,它已经给了我们一些属性和方法,但是我们得重写他们!

Code
public class DomainRoute : Route
{
//
public string Domain { get; set; }
//
public override RouteData GetRouteData(HttpContextBase httpContext)
{
// 构造regex
domainRegex = CreateRegex(Domain);
pathRegex = CreateRegex(Url);
// 请求信息
string requestDomain = httpContext.Request.Headers["host"];
if (!string.IsNullOrEmpty(requestDomain))
{
if (requestDomain.IndexOf(":") > 0)
{
requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":"));
}
}
else
{
requestDomain = httpContext.Request.Url.Host;
}
string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
//匹配域名和路由
Match domainMatch = domainRegex.Match(requestDomain);
Match pathMatch = pathRegex.Match(requestPath);
// Route 数据
RouteData data = null;
if (domainMatch.Success && pathMatch.Success)
{
data = new RouteData(this, RouteHandler);
// 添加默认选项
if (Defaults != null)
{
foreach (KeyValuePair<string, object> item in Defaults)
{
data.Values[item.Key] = item.Value;
}
}
// 匹配域名路由
for (int i = 1; i < domainMatch.Groups.Count; i++)
{
Group group = domainMatch.Groups[i];
if (group.Success)
{
string key = domainRegex.GroupNameFromNumber(i);
if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
{
if (!string.IsNullOrEmpty(group.Value))
{
data.Values[key] = group.Value;
}
}
}
}
// 匹配域名路径
for (int i = 1; i < pathMatch.Groups.Count; i++)
{
Group group = pathMatch.Groups[i];
if (group.Success)
{
string key = pathRegex.GroupNameFromNumber(i);
if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
{
if (!string.IsNullOrEmpty(group.Value))
{
data.Values[key] = group.Value;
}
}
}
}
}
return data;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return base.GetVirtualPath(requestContext, RemoveDomainTokens(values));
}
public DomainData GetDomainData(RequestContext requestContext, RouteValueDictionary values)
{
// 获得主机名
string hostname = Domain;
foreach (KeyValuePair<string, object> pair in values)
{
hostname = hostname.Replace("{" + pair.Key + "}", pair.Value.ToString());
}
// Return 域名数据
return new DomainData
{
Protocol = "http",
HostName = hostname,
Fragment = ""
};
}
// 
}
哇,这是一串按照我们定义的route转换传入请求的URL到tokens的代码,我们这样做是转换{controller}和按照regex然后再尝试匹配route规则,在我们的DomainRoute class里还有其他的helper方法,需要更多的功能可以自己研究扩展。
附代码:附件
(如果要在使用Visual Studio开发Web服务器,务必添加把二级域名添加到hosts文件)(貌似本地测试不用)
原文地址:http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
其实有的人为什么要这么麻烦用这种方式,URL重写或者二级域名直接绑定都可以。但是既然微软给url rouring,就应该能做到。
posted @ 2009-08-12 14:32 大陆响尾蛇 阅读(2161) 评论(14)
编辑

2009年7月31日
SQL Server2005失败:error40;无法打开到SQL Server的连接,设置了Tcp/IP等也不能连接的问题.
TCP/IP设置为允许了.
用户名和密码也正确.
SqlServer2005自带的管理工具可以连接.
连接字符串的服务器地址: . (local) localhost 127.0.0.1 ip地址
都写了没用.
后来打开
配置工具->SQL Server Configuration Manager
找到 sqlserver的网络配置
下面的 TCP/IP协议 右键->属性->IPALL->端口 设置为1433就OK了
截图如下
posted @ 2009-07-31 20:22 大陆响尾蛇 阅读(317) 评论(0)
编辑

2007年4月19日
摘要: SQL Server类型与dotnet类型的对应关系
阅读全文
posted @ 2007-04-19 09:12 大陆响尾蛇 阅读(131) 评论(0)
编辑

2006年12月11日
select a.smallID,a.value,c.name ,a.id
from SysProperties a
inner join syscolumns c on a.id=c.id and a.smallid=c.colid
inner join sysobjects o on a.id=o.id
where o.name = '表名' and c.name = '字段名'
如果只是获得注释的值的话,只要a.value 字段就可以 了。
SysProperties为系统注释表
posted @ 2006-12-11 12:46 大陆响尾蛇 阅读(169) 评论(4)
编辑

2006年11月24日
摘要: 先在APP_Data文件夹下新建一个txt文件:(输入一些内容先,用逗号隔开)id,nameee,test1,01,ddd2,xian,ff3,陆,tffest在页面拉一个GridView控件,Page_Load里写:protectedvoidPage_Load(objectsender,EventArgse){stringConnectionString;stringSQLString;Conn...
阅读全文
posted @ 2006-11-24 15:36 大陆响尾蛇 阅读(438) 评论(0)
编辑

2006年10月24日
摘要: 一、大力敲击回车键
二、在键盘上面吃零食,喝饮料
三、光碟总是放在光驱里(还有看vcd时,暂停后出玩或吃饭)
四、开机箱盖运行
五、用手摸屏幕
六、一直使用同一张墙纸或具有静止画面的屏保
七、拿电脑主机来垫脚
八、给你的计算机抽二手烟
九、装很多测试版的或者共享版的软件
十、在系统运行中进行非正常重启
十一、不扫描和整理硬盘
十二、虚拟内存不指定范围
十三、不用卸载,而是直接删除文件夹
十四、加载或者安装太多同样功能的软件
十五、电脑清理
。。。。。。
阅读全文
posted @ 2006-10-24 13:49 大陆响尾蛇 阅读(180) 评论(0)
编辑
摘要: 清晨最不该吃的三种食物
1.忌喝大量冰凉的饮料:温度相差太大会强烈刺激胃肠道,导致突发性挛缩。
2.忌空腹吃香蕉:香蕉中除了含有助眠的钾,还含有大量的镁元素,若空腹
食用,会使血液中的含镁量骤然升高,而镁是影响心脏功能的敏感元素之一。
3.忌空腹吃菠萝:菠萝里含有强酵素,空腹吃会伤胃,其营养成分必须在吃完
饭后才能更好地被吸收。
知道了清晨饮食的禁忌,就该立刻行动起来,寻找一款适合自己的养胃方案,
这里推荐几款,供大家选择:
阅读全文
posted @ 2006-10-24 13:47 大陆响尾蛇 阅读(256) 评论(0)
编辑
摘要: 一.运动1.每天必须运动30分钟以后,温和的有氧运动,活动全身2.每隔1个半小时,起身活动腰部,颈部,肩部3.经常活动手指,由于手指长期握鼠标,会造成末梢神经炎4.经常眨眼睛,可以缓解眼部肌肉的紧张,而且促进眼部血液流通,是眼睛表面更加湿润,减少干涩,有效的保护眼睛5.点缓解眼疲劳的眼药水时,请认真看说明书,按剂量使用,切忌不可超剂量使用,对眼睛有危害,最好通过眨眼和做眼保健操,缓解眼部疲劳6.为...
阅读全文
posted @ 2006-10-24 13:44 大陆响尾蛇 阅读(191) 评论(1)
编辑

2006年10月20日
摘要: 真想不明白为什么要这样做。源地址:http://tech.qq.com/a/20061020/000215.htm昨日,中国互联网协会行业自律工作委员会秘书长杨君佐确认了中国互联网协会在推进博客实名制的消息。在网络上沸沸扬扬的博客实名制已成定局。 据悉,中国互联网协会受信息产业部委托开展“博客实名制”的研究,研究成果将提交信产部供决策参考。 博客实名制,意味着博客用户必须进...
阅读全文
posted @ 2006-10-20 13:01 大陆响尾蛇 阅读(131) 评论(2)
编辑