小结 路由静态实现

js小结

1.给元素挂事件

elem.attachEvent(("onchange", fun);

上面的好像只能在IE中使用

elem.onchange=cheng;

function cheng(){};

2.部替换掉匹配的字符(g为全局标志)

string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);

string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。

 

 

 

c#

路由配置,设置页面地址,类似伪静态

 

添加 System.Web.Routing和System.Web.Abstractions 引用

 

public class Global : HttpApplication
    {

        
protected void Application_Start(object sender, EventArgs e)
        {
            
//程序运行加载路由
            RegisterRoutes(RouteTable.Routes);
        }

        
/// <summary>
        
/// 注册路由
        
/// </summary>
        
/// <param name="routes"></param>
        private void RegisterRoutes(RouteCollection routes)
        {

            
//访问:MemInfo/7 就相当于访问 Home/MemInfo.aspx?Id=7
            routes.Add("测试1"
                       
new Route("MemInfo/{Id}",
                                 
new RouteValueDictionary { { "Id""0" } },//默认0.
                                 new RouteValueDictionary { { "Id"@"^[1-9]\d*$" } },//定义规则
                                 new WebFormRouteProvider("~/Home/MemInfo.aspx")));//物理路径
            
//以下配置自定义规则.



        }


        
/// <summary>
        
/// 当程序出错的时候系统需要执行的代码!
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            
//在部署的时候需要修改为!DEBUG
#if DEBUG
            var errorLog 
= "~/App_Data/Error.log";

            var errorLogPath
=Server.MapPath(errorLog);
            
//获取最后一个Exception
            var error = Server.GetLastError().InnerException;

            
if (error == nullreturn;

            
lock (this)//永久锁定日志.
            {
                
if (!System.IO.File.Exists(errorLogPath))
                {
                    System.IO.File.Create(errorLogPath).Close();
                }
                
using (var sw =
                    
new StreamWriter(errorLogPath, true, Encoding.Default))
                {
                    sw.Write(
string.Format("[{0}] ---- {1}", DateTime.Now, error.Message));
                    sw.Write(sw.NewLine);
                    sw.WriteLine(error);
                    sw.WriteLine(
"----------------------------------------------------------");
                }
            }

            
//Response.Write(error);
            
//Response.End();
#endif

        }

     }
//下面是路由demo.
            
//Response.Write(Items["action"]);
            var id = this.Context.Items["Id"];
            
            
int.TryParse(id.ToString(), out _id);
            BindMem(_id);

            
if (User.Identity.IsAuthenticated)
            {
                
int memId = 0;
                
int.TryParse(GS.Common.CookiesHelper.GetCookieValue("MemId"), out memId);
                
if (memId == _id)
                {
                    _IsMy 
= true;
                }
            }

 

 

 

/// <summary>
    
/// 路由配置
    
/// </summary>
    public class WebFormRouteProvider : IRouteHandler
    {
        
/// <summary>
        
/// 重写的虚拟路径
        
/// </summary>
        public string VirtualPath { getprivate set; }

        
/// <summary>
        
/// 实例化WebPage路由
        
/// </summary>
        
/// <param name="virtualPath"></param>
        public WebFormRouteProvider(string virtualPath)
        {
            VirtualPath 
= virtualPath;
        }

        
public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
// 从ReqestContext对象中获取URL参数,
            
//并把值写到HttpContext的Items集合中供路由目标页面使用 
            
//this.Item["action"] 就可以获取了.
            foreach (var urlParm in requestContext.RouteData.Values)
            {
                requestContext.HttpContext.Items[urlParm.Key] 
= urlParm.Value;
            }

            
return (IHttpHandler)
                BuildManager.CreateInstanceFromVirtualPath(VirtualPath, 
typeof(IHttpHandler));
        }
    }

 

posted @ 2011-02-25 11:36  会飞的剑  阅读(292)  评论(0编辑  收藏  举报