ASP.NET之路由
说来惭愧,已经做了几个公司的项目,但是还没有系统的学习过ASP.NET MVC。就简单的凭以前的经验,然后遇到问题google一下,竟然也做完了项目。现在有点小空,准备系统的看一下MVC,下面记录一些我的学习笔记,方便以后查阅。
1. 当你运行一个ASP.NET MVC的项目时,一个路由表在程序运行一开始就已经建立了。相关的代码在global.asax里面。程序一开始会与性Application_Start(), 注册你的路由规则到RouteTable.Routes中去。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;d using System.Web.Routing; namespace MVCApplication1 { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); } } }
程序就会调用HomeController.Index(3)
如何增加一个自定义的路由
比如你需要显示一个归档的blog列表。你希望URL是这样的Archive/04-07-2011,那么你的路由则应该如此定义。
routes.MapRoute( "Blog", // Route name "Archive/{entryDate}", // URL with parameters new { controller = "Archive", action = "Entry" } // Parameter defaults );
当你输入Archive/04-07-2011,程序会执行到ArchiveController.Entry(DateTime enteryDate)。 那么这时候如果你输入的Archive/MVC, 你就会得到一个报错,因为"MVC"只是一个string类型,并不是DateTime的。
如何做到避免这种情况的发生呢。就需要给这个路由建立一个约束。
如何给路由建立一个约束
routes.MapRoute(
" Blog " , // Route name
" Archive/{entryDate} " , // URL with parameters
new { controller = " Archive " , action = " Entry " }, // Parameter defaults
new { entryDate = @" \d{2}-\d{2}-\d{4} " }
);
当然了,我这个日期的约束是简单的约束,只是举个例子。
这样,那些不符合我们要求的URL,比如Archive/MVC,就不会匹配到这个路由了,它就会去找其他的路由来匹配,当然如果匹配不到的话,还是会报错的。
好了,今天就记录到这里。后面看到了新的再写了:)
1. 首先使用Configuration Console, 在blocks里面 Add Caching Setteings. 设置一些参数,比如Expiration Polling Frequency(seconds), 这个是指每隔指定的时间,cache block会检查那些过期的cache item,并且把他们删除掉。
2. 配置好以后可以保存为一个config文件,然后把config内容拷贝到你的project中的配置文件中。
<configSections>
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<cachingConfiguration defaultCacheManager="Data Cache Manager">
<cacheManagers>
<add name="Data Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="3600" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
3. 在你的project中引入一些dll。
Microsoft.Practices.EnterpriseLibrary.Caching.dll
Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll
Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll
4. 现在就可以使用cache了,下面的是一个简单的使用cache功能的方法。
private Instance GetInstanceCache(string guid)
{
Instance ins = null;
string key = "INS" + guid;
ICacheManager defaultCache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>();
if (defaultCache.Contains(key))
{
object cache = defaultCache.GetData(key);
if (cache != null)
{
ins = cache as Instance;
}
else
{
ins = BLInstance.GetInstanceByGuid(guid);
defaultCache.Remove(key);
defaultCache.Add(key, ins, CacheItemPriority.High, null, new SlidingTime(new TimeSpan(int.Parse(ConfigurationManager.AppSettings["ExpirationTimeHours"]), int.Parse(ConfigurationManager.AppSettings["ExpirationTimeMinutes"]), int.Parse(ConfigurationManager.AppSettings["ExpirationTimeSeconds"]))));
}
}
else
{
ins = BLInstance.GetInstanceByGuid(guid);
defaultCache.Add(key, ins, CacheItemPriority.High, null, new SlidingTime(new TimeSpan(int.Parse(ConfigurationManager.AppSettings["ExpirationTimeHours"]), int.Parse(ConfigurationManager.AppSettings["ExpirationTimeMinutes"]), int.Parse(ConfigurationManager.AppSettings["ExpirationTimeSeconds"]))));
}
return ins;
}
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息