asp.net mvc url的重写实现伪静态

时间 2017/9/29 0:00:48 加载中...

目的:

将原来的“/Home/Detail/85”的地址形式修改为类似“/articles/asp.net-mvc-ueditor-img-uploader.html”的形式,这样除了形式上美观外,也有利于SEO

这也是实现了伪静态,即看起来像直接访问的静态文件,其实还是从数据库中读取的数据。


原理:

在ASP.NET MVC中,实现伪静态主要用到了“路由”,通过配置“路由”来匹配对应的控制器即可。

但像“/articles/asp.net-mvc-ueditor-img-uploader.html”的地址如何查询记录呢?

其实是将“articles”设定为路由模版的一部分,始终不变,而后面的“asp.net-mvc-ueditor-img-uploader”是记录中的一个字段,可以命名为UrlTitle。

通过后面的名字“asp.net-mvc-ueditor-img-uploader”充当唯一标识来查找记录。


步骤:

1.在 RouteConfig 文件的 RegisterRoutes 方法中添加新的路由。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    ////这个不要打开,即访问存在的文件直接访问,打开后,在项目根目录下的.ashx文件不能访问,且使用Bundle引用的css、js文件不能访问
    //routes.RouteExistingFiles = true;
    CustomRoute(routes);//这里是新加的路由

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "Blog.Controllers" }
    );
}

private static void CustomRoute(RouteCollection routes)
{    
    routes.MapRoute(
        name: "Articles",
        url: "articles.html",
        defaults: new { Controller = "Home", action = "Index" },
        namespaces: new[] { "Blog.Controllers" }
    );
    routes.MapRoute(
        name: "Articles-p",
        url: "articles/p{page}.html",
        defaults: new { Controller = "Home", action = "Index" },
        namespaces: new[] { "Blog.Controllers" }
    );
    routes.MapRoute(
        name: "Articles-view",
        url: "articles/{urlTitle}.html",
        defaults: new { Controller = "Home", action = "ViewArticle" },
        namespaces: new[] { "Blog.Controllers" }
    );
}

定义的新的路由一定要在默认路由的前面。

第2个路由 url: "articles/p{page}.html",defaults: new { Controller = "Home", action = "Index" } 的意思是:

访问类似 articles/p1.html 的地址时,走的是控制器 /home/index方法,且此方法中参数“page”的值为1。注意url模版中的名字要和方法中的参数名字一致。


public ActionResult Index(int? page = 1)
{
    ArticleListQuery listModel = new ArticleListQuery();
    listModel.PageIndex = Convert.ToInt32(page);
    listModel.PageSize = 10;

    ArticleListModelResult result = _articleService.GetPaged(listModel);
    var pageList = new StaticPagedList(result.List, listModel.PageIndex, listModel.PageSize, result.TotalCount);

   return View(pageList);
}


第三个路由url: "articles/{urlTitle}.html",defaults: new { Controller = "Home", action = "ViewArticle" }的意思是:

访问类似 articles/asp.net-mvc-ueditor-img-uploader.html 的地址时,走的是控制器 /home/ViewArticle方法,且此方法中参数“urlTitle”的值为asp.net-mvc-ueditor-img-uploader。注意url模版中的名字要和方法中的参数名字一致。


public ActionResult ViewArticle(string urlTitle)
{
    if (string.IsNullOrEmpty(urlTitle))
        return Content("参数呢?你吃了?");
    Article model = _articleService.GetByUrlTitle(urlTitle);            
    return View("Detail",model);
}



2. 新加路由后,访问类似 /articles/asp.net-mvc-ueditor-img-uploader.html 的地址提示文件不存在,是因为没有走Module,直接访问的文件。

需要在配置文件中的“<handlers>”节点添加配置:

<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>

这样访问html结尾的url,就会走我们的控制器了。


总结一下,共两步:

  1. 新增路由

  2. 添加配置文件

完成

版权说明
作者:SQBER
文章来源:http://sqber.com/articles/asp.net-mvc-url-rewrite.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。