asp.net mvc项目中实现伪静态
2018-01-05 10:26
387 查看
前沿
asp.net mvc开发的网站,为了对SEO优化友好,且为了页面的URL地址看起来更好看些,就首先尝试了一下伪静态的实现,在此记录,以备忘~~下载安装插件
在asp.net mvc中实现伪静态化,需要借助UrlRewrite.dll库,所以首先要使用nuget下载安装,如下图所示:安装完成后,会自动在webconfig中添加如下的配置信息:
WebConfig配置
然后我们需要手动添加一些配置信息,如下图:相应的代码如下:
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter" />
<CustomConfiguration>
<urls>
<!--([\w]+)表示,1到n个字母或数字或下划线或汉字组成-->
<add virtualUrl="~/Index.html" destinationUrl="~/Home/Index" />
<add virtualUrl="~/(\d+)/Detail.html" destinationUrl="~/Home/Detail/?guid=$1" />
</urls>
</CustomConfiguration>
路由配置
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute( // 不带参数的伪静态路由
name: "Index.html",
url: "{controller}/{action}.html",
defaults: new { controller = "Home", action = "Index" },
namespaces: new[] { "Site.WebApp.Controllers" }
);
routes.MapRoute(
"IDHtml", // 只有一个参数id的伪静态路由
"{controller}/{action}/{id}.html",// 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Site.WebApp.Controllers" }
);
routes.MapRoute(//两个参数(id和pid)不带动作,伪静态
"TwoparameterNoAction",
"{controller}/{action}/{id}/{pid}.html",
new { controller = @"[a-zA-Z]", action = "Index", id = @"[\d]{0,6}", pid = UrlParameter.Optional },
namespaces: new[] { "Site.WebApp.Controllers" }
);
routes.MapRoute( // 默认路由配置
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Site.WebApp.Controllers" }
);
}
效果预览
个人博客文章地址:https://www.dupengnet.com/detail/5a4ee9b9bc1f6a0c046b86a3
相关文章推荐
- ASP.NET MVC 伪静态的实现
- 利用CSS预处理技术实现项目换肤功能-(less css+asp.net mvc bundle)
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(5.5) 登录功能的实现,完善登录功能
- C# Asp.Net MVC 3 项目实现控制器(controller)与页面(view)分项目管理
- C# Asp.Net MVC 3 项目实现控制器(controller)与页面(view)分项目管理
- Asp.Net MVC3 简单入门第一季(五) 通过Asp.Net MVC的区域功能实现将多个MVC项目部署到一个站点
- MVC项目实践,在三层架构下实现SportsStore-09,ASP.NET MVC调用ASP.NET Web API的查询服务
- 通过Asp.Net MVC的区域功能实现将多个MVC项目部署到一个站点
- NHibernate3.2+Asp.net MVC3+Extjs 4.0.2项目实践(六):Extjs Grid,Window,Form实现增删改操作
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(5.1) 登录功能的实现,开始接触Spring IOC、DI
- NHibernate3.2+Asp.net MVC3+Extjs 4.0.2项目实践(二): NHibernate数据访问层实现
- 通过Asp.Net MVC的区域功能实现将多个MVC项目部署
- 如何让你的ASP.NET MVC项目实现AJAX
- [ASP.NET MVC] 利用自定义的AuthenticationFilter实现Basic认证
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
- ASP.NET MVC AJAX实现 以及如何处理json数据,简介
- ASP.NET MVC中将控制器分离到类库的实现(转)
- Asp.net MVC 示例项目"Suteki.Shop"分析之---结束篇
- asp.net mvc项目创建WebApi简单例子