今天搞了一下MVC,看到Routing的配置都是写在Global.asax文件里,心里想了想,如果很多的路径的话,那Global.asax文件不会是很大很大,所以就想想能不能把路径之类的写到一个配置文件里,每次只需要修改一下配置文件就可以了。按着这个思路去网上找了一下,果然找到了一个解决方案,是老外写的MvcXmlRouting,拿来用了一下,路由倒是配置到了RouteConfiguration.config文件里,具体做法如下:
先在web.config文件里加上配置
<
configSections
>
< section name ="RouteConfiguration" type ="MvcXmlRouting.MvcRouteConfigurationSection" />
</ configSections >
< RouteConfiguration configSource ="RouteConfiguration.config" />
< section name ="RouteConfiguration" type ="MvcXmlRouting.MvcRouteConfigurationSection" />
</ configSections >
< RouteConfiguration configSource ="RouteConfiguration.config" />
然后在Global.asax文件里用下面这个方法替换到原来的配置方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
static
void
RegisterRoutes(RouteCollection routes)
{
MvcRouteConfigurationSection section =
(MvcRouteConfigurationSection)ConfigurationManager.GetSection( " RouteConfiguration " );
routes.MapRoute(section);
}
{
MvcRouteConfigurationSection section =
(MvcRouteConfigurationSection)ConfigurationManager.GetSection( " RouteConfiguration " );
routes.MapRoute(section);
}
上面这些都弄完了之后,接下来就只用写RouteConfiguration.config文件了,里面大致如下写
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
RouteConfiguration
>
< ignore >
<!-- 忽略对.axd文件的Route,直接处理 -->
< add url ="{resource}.axd/{*pathInfo}" >
< constraints >
< add name ="resource" value ="\w+" />
</ constraints >
</ add >
</ ignore >
< map >
<!-- 使用频率越高的路由要放到越前面!!!性能 -->
<!-- 首页 -->
< route name ="Home" url ="" controller ="Home" action ="Index" >
</ route >
<!-- 重置密码 -->
< route name ="RecoverPassword" url ="RecoverPassword/{code}/{pageIndex}" controller ="User" action ="RecoverPassword" >
< parameters >
<!-- 参数配置 w是字符串,d是数字 -->
< add name ="code" value ="" constraint ="\w+" />
< add name ="pageIndex" value ="1" constraint ="\d+" />
</ parameters >
</ route >
</ map >
</ RouteConfiguration >
< ignore >
<!-- 忽略对.axd文件的Route,直接处理 -->
< add url ="{resource}.axd/{*pathInfo}" >
< constraints >
< add name ="resource" value ="\w+" />
</ constraints >
</ add >
</ ignore >
< map >
<!-- 使用频率越高的路由要放到越前面!!!性能 -->
<!-- 首页 -->
< route name ="Home" url ="" controller ="Home" action ="Index" >
</ route >
<!-- 重置密码 -->
< route name ="RecoverPassword" url ="RecoverPassword/{code}/{pageIndex}" controller ="User" action ="RecoverPassword" >
< parameters >
<!-- 参数配置 w是字符串,d是数字 -->
< add name ="code" value ="" constraint ="\w+" />
< add name ="pageIndex" value ="1" constraint ="\d+" />
</ parameters >
</ route >
</ map >
</ RouteConfiguration >
接下来就可以像正常那样输入地址访问了。
不过我现在遇到了一个问题,就是在处理Ajax请求的时候,他往往报路劲不可访问的错误。在正常的配置路由时对于AJAX的请求路径是不需要配置的,按照老外的说明文档也是说不需要配置的,可是现在就是出现了路径不可访问的错误,找了老半天也没能找出原因来,不知道哪位同学能解决这个问题
文章不才,发到首页,还请手下留情