MVC配置伪静态

提出需求

伪静态能提高搜索引擎收录,还不影响硬盘寿命,是一个不错的选择,但是会增加CPU和内存开销,由于时候也需要实现伪静态。

web.config配置

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

修改路由配置

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}.html",
                defaults: new { controller = "Home", action = "Index" }
            );

参数传值

home/index.html?page=1
home/index/1.html  (这种写法无法自定义参数名称,只能使用路由里配置的参数名称)

如果URL没有page=1会报错,屏蔽报错用以下方法

        public ActionResult Welcome(int? page)
        {
            ViewBag.Message = ""+page+"";
            return View();
        }

或者直接给默认值

        public ActionResult Welcome(int page=1)
        {
            ViewBag.Message = ""+page+"";
            return View();
        }

 

posted @ 2015-04-11 14:41  哆啦无梦  阅读(411)  评论(0编辑  收藏  举报