Routes是如何把URL映射为Action
-请求路由管道
请求管道概述
1.UrlRotingModule视图使用RouteTable里的注册路由当前请求
2.如果匹配成功,则从路由对象生成IRouteHandler对象
3.Routing模块调用IRouteHandler中的GetHandler方法,这一方法返回一个IHttpHandler
4.ProcessRequest对象调用Http handler对象响应
5.在MVC架构中IRouteHandler对象默认是一个MvcRouteHandler对象,它返回的对象是一个MvcHandler对象
路由匹配法则
-routedata
{foo}/{bar}/{baz}
Public Interface IRouteConstraint{
BoolMatch(HttpContextBasehttpContext,
Route route,
string parameterName,
RouteValuedictionaryvalues,
RouteDirectionrouteDirection);
}
routes.MapRoute(“name”,”{controller}”,null,new{httpMethod=new HttpMethonConstraint(“GET”)});
Route扩展
routes.MapResources(“Products”);
描述信息
/Products 显示所有products
/Products/new 生成一个表格用于添加新纪录
/Products/1 显示ID是1的记录
/Products/1/edit 编辑ID是1的记录
public class RestRoute:RouteBase{
public override RouteDataGetRouteData
(HttpContextBasehttpcontext){}
public override VirtualPathDataGetVirtualPath
(RequestContextrequestcontext,
RouteValueDictionaryvalues){}
}
List<Route> _internalRoutes= new List<Route>();
public string Resource { get; private set; }
public RestRoute(string resource)
{
this.Resource= resource;
MapRoute(resource, “index”, “GET”, null);
MapRoute(resource, “create”, “POST”, null);
MapRoute(resource + “/new”, “newitem”, “GET”, null);
MapRoute(resource + “/{id}”, “show”, “GET”, new { id = @”\d+” });
MapRoute(resource + “/{id}”, “update”, “PUT”, new { id = @”\d+” });
MapRoute(resource + “/{id}”, “delete”, “DELETE”, new { id = @”\d+” });
MapRoute(resource + “/{id}/edit”, “edit”, “GET”, new { id = @”\d+” });
}
public void MapRoute(string url, string actionName, string httpMethod,
object constraints)
{
RouteValueDictionaryconstraintsDictionary;if (constraints != null)
{
constraintsDictionary= new RouteValueDictionary(constraints);
}
else
{
constraintsDictionary= new RouteValueDictionary();
}
constraintsDictionary.Add(“httpMethod”, new HttpMethodConstraint(httpMethod));
_internalRoutes.Add(new Route(url, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new
{ controller = Resource, action = actionName}),
Constraints = constraintsDictionary
});
}
public override RouteDataGetRouteData(HttpContextBasehttpContext)
{
foreach(varroute in this._internalRoutes)
{
varrvd= route.GetRouteData(httpContext);
if (rvd!= null) return rvd;
}
return null;
}
public override VirtualPathDataGetVirtualPath(RequestContextrequestContext,
RouteValueDictionaryvalues)
{
foreach(varroute in this._internalRoutes)
{
VirtualPathDatavpd= route.GetVirtualPath(requestContext, values);
if (vpd!= null) return vpd;
}
return null;
}
Routes.Add(new RestRoute("Products"));
编辑Routes
Protected void Application_start(){
AreaRegistration.RegisterAllAreas();
RouteTable.Routes.registerRoutes(“~/Config/routes.cs”);
}
using System.Web.Mvc;
using System.Web.Routing;
using EditableRoutesWeb;
// Use this one for Full Trust
public class Routes : IRouteRegistrar
{public void RegisterRoutes(RouteCollectionroutes){
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(“Default”,“{controller}/{action}/{id}”,new {
controller = “Home”,
action = “Index”,
id = ““ });}}
使用Cache监控代码
using System;
using System.Web.Compilation;
using System.Web.Routing;
namespace EditableRoutesWeb{
public static class RouteRegistrationExtensions{
public static void RegisterRoutes(
this RouteCollectionroutes,stringvirtualPath){
ConfigFileChangeNotifier.Listen(
virtualPath, vp=> routes.ReloadRoutes(vp));
}
static void ReloadRoutes(this RouteCollectionroutes, string virtualPath)
{
varassembly = BuildManager.GetCompiledAssembly(virtualPath);
var registrar = assembly.CreateInstance(“Routes”) as IRouteRegistrar;
using(routes.GetWriteLock())
{
routes.Clear();
registrar.RegisterRoutes(routes);
}}}}
怎么知道有改变?
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
namespace EditableRoutesWeb{
public class ConfigFileChangeNotifier{
private ConfigFileChangeNotifier(Action<string> changeCallback)
: this(HostingEnvironment.VirtualPathProvider, changeCallback){}
private ConfigFileChangeNotifier(VirtualPathProvidervpp,
Action<string> changeCallback) {
_vpp= vpp;
_changeCallback= changeCallback;
}
VirtualPathProvider_vpp;
Action<string> _changeCallback;
// When the file at the given path changes, we’ll call the supplied action.
public static void Listen(string virtualPath, Action<string> action) {
varnotifier= new ConfigFileChangeNotifier(action);
notifier.ListenForChanges(virtualPath);
}
void ListenForChanges(string virtualPath) {
// Get a CacheDependencyfrom the BuildProvider, so that we know
// anytime something changes
varvirtualPathDependencies= new List<string>();
virtualPathDependencies.Add(virtualPath);
CacheDependencycacheDependency= _vpp.GetCacheDependency(
virtualPath, virtualPathDependencies, DateTime.UtcNow);
HttpRuntime.Cache.Insert(virtualPath/*key*/,
virtualPath/*value*/,
cacheDependency,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(OnConfigFileChanged));
}
void OnConfigFileChanged(string key, object value,
ChItRdR) {...
2011-4-17 14:50 danny
-请求路由管道
请求管道概述
1.UrlRotingModule视图使用RouteTable里的注册路由当前请求
2.如果匹配成功,则从路由对象生成IRouteHandler对象
3.Routing模块调用IRouteHandler中的GetHandler方法,这一方法返回一个IHttpHandler
4.ProcessRequest对象调用Http handler对象响应
5.在MVC架构中IRouteHandler对象默认是一个MvcRouteHandler对象,它返回的对象是一个MvcHandler对象
路由匹配法则
-routedata
{foo}/{bar}/{baz}
Public Interface IRouteConstraint{
BoolMatch(HttpContextBasehttpContext,
Route route,
string parameterName,
RouteValuedictionaryvalues,
RouteDirectionrouteDirection);
}
routes.MapRoute(“name”,”{controller}”,null,new{httpMethod=new HttpMethonConstraint(“GET”)});
Route扩展
routes.MapResources(“Products”);
描述信息
/Products 显示所有products
/Products/new 生成一个表格用于添加新纪录
/Products/1 显示ID是1的记录
/Products/1/edit 编辑ID是1的记录
public class RestRoute:RouteBase{
public override RouteDataGetRouteData
(HttpContextBasehttpcontext){}
public override VirtualPathDataGetVirtualPath
(RequestContextrequestcontext,
RouteValueDictionaryvalues){}
}
List<Route> _internalRoutes= new List<Route>();
public string Resource { get; private set; }
public RestRoute(string resource)
{
this.Resource= resource;
MapRoute(resource, “index”, “GET”, null);
MapRoute(resource, “create”, “POST”, null);
MapRoute(resource + “/new”, “newitem”, “GET”, null);
MapRoute(resource + “/{id}”, “show”, “GET”, new { id = @”\d+” });
MapRoute(resource + “/{id}”, “update”, “PUT”, new { id = @”\d+” });
MapRoute(resource + “/{id}”, “delete”, “DELETE”, new { id = @”\d+” });
MapRoute(resource + “/{id}/edit”, “edit”, “GET”, new { id = @”\d+” });
}
public void MapRoute(string url, string actionName, string httpMethod,
object constraints)
{
RouteValueDictionaryconstraintsDictionary;if (constraints != null)
{
constraintsDictionary= new RouteValueDictionary(constraints);
}
else
{
constraintsDictionary= new RouteValueDictionary();
}
constraintsDictionary.Add(“httpMethod”, new HttpMethodConstraint(httpMethod));
_internalRoutes.Add(new Route(url, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new
{ controller = Resource, action = actionName}),
Constraints = constraintsDictionary
});
}
public override RouteDataGetRouteData(HttpContextBasehttpContext)
{
foreach(varroute in this._internalRoutes)
{
varrvd= route.GetRouteData(httpContext);
if (rvd!= null) return rvd;
}
return null;
}
public override VirtualPathDataGetVirtualPath(RequestContextrequestContext,
RouteValueDictionaryvalues)
{
foreach(varroute in this._internalRoutes)
{
VirtualPathDatavpd= route.GetVirtualPath(requestContext, values);
if (vpd!= null) return vpd;
}
return null;
}
Routes.Add(new RestRoute("Products"));
编辑Routes
Protected void Application_start(){
AreaRegistration.RegisterAllAreas();
RouteTable.Routes.registerRoutes(“~/Config/routes.cs”);
}
using System.Web.Mvc;
using System.Web.Routing;
using EditableRoutesWeb;
// Use this one for Full Trust
public class Routes : IRouteRegistrar
{public void RegisterRoutes(RouteCollectionroutes){
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(“Default”,“{controller}/{action}/{id}”,new {
controller = “Home”,
action = “Index”,
id = ““ });}}
使用Cache监控代码
using System;
using System.Web.Compilation;
using System.Web.Routing;
namespace EditableRoutesWeb{
public static class RouteRegistrationExtensions{
public static void RegisterRoutes(
this RouteCollectionroutes,stringvirtualPath){
ConfigFileChangeNotifier.Listen(
virtualPath, vp=> routes.ReloadRoutes(vp));
}
static void ReloadRoutes(this RouteCollectionroutes, string virtualPath)
{
varassembly = BuildManager.GetCompiledAssembly(virtualPath);
var registrar = assembly.CreateInstance(“Routes”) as IRouteRegistrar;
using(routes.GetWriteLock())
{
routes.Clear();
registrar.RegisterRoutes(routes);
}}}}
怎么知道有改变?
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
namespace EditableRoutesWeb{
public class ConfigFileChangeNotifier{
private ConfigFileChangeNotifier(Action<string> changeCallback)
: this(HostingEnvironment.VirtualPathProvider, changeCallback){}
private ConfigFileChangeNotifier(VirtualPathProvidervpp,
Action<string> changeCallback) {
_vpp= vpp;
_changeCallback= changeCallback;
}
VirtualPathProvider_vpp;
Action<string> _changeCallback;
// When the file at the given path changes, we’ll call the supplied action.
public static void Listen(string virtualPath, Action<string> action) {
varnotifier= new ConfigFileChangeNotifier(action);
notifier.ListenForChanges(virtualPath);
}
void ListenForChanges(string virtualPath) {
// Get a CacheDependencyfrom the BuildProvider, so that we know
// anytime something changes
varvirtualPathDependencies= new List<string>();
virtualPathDependencies.Add(virtualPath);
CacheDependencycacheDependency= _vpp.GetCacheDependency(
virtualPath, virtualPathDependencies, DateTime.UtcNow);
HttpRuntime.Cache.Insert(virtualPath/*key*/,
virtualPath/*value*/,
cacheDependency,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(OnConfigFileChanged));
}
void OnConfigFileChanged(string key, object value,
ChItRdR) {...
2011-4-17 14:50 danny
发表评论
-
10.2Asp.net MVC各层使用TDD方式
2011-05-01 12:09 804Asp.net MVC各层使用TDD方式 Asp.net M ... -
10.1TDD简介
2011-04-30 23:09 558MVC中的测试驱动开发 为什么需要TDD TDD的由来 它 ... -
9、MVC程序安全限定
2011-04-29 22:14 1811常见网络安全攻击隐患 针对Asp.net MVC防御体系 安 ... -
8.2ASP.NET MVC2使用Ajax类库实例
2011-04-29 13:05 923ASP.NET MVC2使用Ajax类库实例 .net fo ... -
8.1Ajax类库介绍
2011-04-28 09:12 885在ASP.NET MVC中使用 -Microsoft ASP. ... -
7.2自定义开发Filter
2011-04-27 16:04 625Custom Filters ➤IAuthorization ... -
7.1Filter的使用
2011-04-26 22:20 501Filter的使用 -对Action的附加说明 Asp.n ... -
6.3Action的调用与属性
2011-04-26 09:43 666Action的调用与属性 1、唤起Action R ... -
6.2ActionResult的使用
2011-04-25 19:14 683ActionResult public abstract c ... -
6.1Controller类的基本构成
2011-04-25 09:17 669关于Controller Controller是什么? C ... -
5.6在Webform中使用routing
2011-04-23 22:48 669在Webform中使用routing 在Asp.net 4中使 ... -
5.4使用routing生成URL
2011-04-21 11:05 600使用routing生成URL URL生成器概述 1、质询每个 ... -
5.3使用Routes
2011-04-20 09:11 7405.3使用Routes 注册Area Route publi ... -
5.2Routes匹配URL的工作方式
2011-04-19 08:55 852URL的匹配规则 -site/{controller}/{ac ... -
5.1Url和Routes介绍
2011-04-17 12:05 6495.1Url和Routes介绍 关于Urls -域名好记好拼 ... -
4、View引擎介绍
2011-04-16 22:03 649View引擎介绍 Request =>Routing= ... -
3、HtmlHelper类(续)
2011-04-16 13:41 652HtmlHelper类(续) Html.TextArea 用 ... -
2、HtmlHelper类
2011-04-15 22:33 1103HtmlHelper类 <%Html. MVC1 & ... -
1、View层
2011-04-15 09:06 629ViewDataDictionary类 View 负责输出 ...
相关推荐
Action是处理业务逻辑的类,ActionForm用于封装用户输入的数据,ActionMapping则定义了请求URL与Action类之间的映射关系,ActionForward负责控制请求的转发。 在创建登录功能时,我们通常会有一个登录页面(比如...
配置包括指定数据源URL(指向我们的Struts2 Action),定义请求参数,以及设置如何展示建议结果的模板。此外,还要确保引入jQuery库和jQuery UI库,以及相关的CSS样式文件以美化自动补全的建议列表。 最后,为了使...
4. **URL**: 提供了生成和处理URL的辅助函数,如`url()`, `route()`, `action()`等,可以帮助我们构建指向路由或控制器的链接。 5. **Form**: 虽然在Laravel 5.5之后被移除,但在之前的版本中,`Form`助手提供了一...
- 黑盒测试关注的是软件的功能,不考虑其内部结构,主要是验证输入数据与预期输出是否匹配。 - 白盒测试关注的是程序的内部逻辑,通过测试程序的每个路径确保它们按预期工作。 8. **CSS的作用是什么?** - CSS...
- **使用**:通过正则表达式匹配URL模式。 **15.4 默认值** - **定义**:设置路由参数的默认值。 **15.5 匿名/回调路由逻辑** - **机制**:使用匿名函数或回调函数来动态定义路由规则。 **15.6 例子** - **...
5.5节.为Tree设置XML数据 5.6节.为Tree创建项渲染器 5.7节.在Tree控件中使用复杂数据对象 5.8节.只允许List的某一项可被选中 5.9节.为List的项编辑器添加格式化和验证数据 5.10节.跟踪TileList中所有被选中的子节点 ...
//注意action地址,还有enctype要写成multipart/form-data,和method="POST" <form name="uploadform" method="POST" action="./servlet/FileUpload" ENCTYPE="multipart/form-data"> <tr><td width="100%" ...
12.3.3 动作元素(action elements) 376 12.3.4 注释 383 12.4 jsp的隐含对象 383 12.4.1 pagecontext 384 12.4.2 out 385 12.4.3 page 385 12.4.4 exception 386 12.5 对象和范围 387 12.6 留言板程序 389...
12.3.3 动作元素(action elements) 376 12.3.4 注释 383 12.4 jsp的隐含对象 383 12.4.1 pagecontext 384 12.4.2 out 385 12.4.3 page 385 12.4.4 exception 386 12.5 对象和范围 387 12.6 留言板程序 389...
12.3.3 动作元素(action elements) 376 12.3.4 注释 383 12.4 jsp的隐含对象 383 12.4.1 pagecontext 384 12.4.2 out 385 12.4.3 page 385 12.4.4 exception 386 12.5 对象和范围 387 12.6 留言板程序 389...
12.3.3 动作元素(action elements) 376 12.3.4 注释 383 12.4 jsp的隐含对象 383 12.4.1 pagecontext 384 12.4.2 out 385 12.4.3 page 385 12.4.4 exception 386 12.5 对象和范围 387 12.6 留言板程序 389...
当用户访问一个页面时,Magento会根据请求的URL找到对应的Layout文件,并解析该文件来生成页面布局。 **3.8 输出和getChildHtml方法** Block对象提供了许多方法来帮助输出HTML代码,其中`getChildHtml`方法用于...
- **数据类型转换:** Java中的数据类型需与数据库中的数据类型匹配,尤其是对于特殊类型的处理。 - **异常处理:** 使用过程中需要注意捕获并处理可能出现的异常情况。 #### 二、快速上手 **2.1 创建项目** - **...
不过明确的是编写那样的代码有多简单,购买它们会有多昂贵以及它们需要多么昂贵和强大的硬件。如果你有什么中立的观点(比如说没有被SUN和Microsoft的百万美金所影响),请顺便通知我。 据我所知,JSP基于Java,...