asp.net mvc项目中实现伪静态

3 篇文章 0 订阅

前沿

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值