zoukankan      html  css  js  c++  java
  • ASP.NET——URL重写,伪静态

    我们用.net开发的网站,都是动态网页。其扩展名一般是.aspx,ashx等。有的时候还带着参数,如:http://www.NLYJF.com/user/blogs.aspx?id=2123 此类样式的URL,这样的格式对用户来说,很难看,感觉很不舒服。而搜索引擎在收录链接的时候,排名也会靠后。因此我们有必要把URL写成类似于: http://www.NLYJF.com/user/blogs/2123.html 这样的形式。

    在用户输入.html的时候,其实访问的是.aspx格式的网页,只不过中间被“转换”了一下。

    URL重写:

    ①新建一个全局配置文件,Global.asax。

    ②在Global.asax中,在BeginRequest阶段添加代码

    BeginRequest
    void Application_BeginRequest(object sender,EventArgs e)
    {
    //Accept the URL inputed by users
    string url=Request.RawUrl;

    Regex regex=new Regex(@"user/blogs/(\d+).htm");

    Match match=regex.Match(url);
    if(match.Success)
    {

    string id=match.Gourp[1].Value;

    string s="user/blogs.aspx?id="+id;

    HttpContext.Current.RewritePath(s);
    }
    }

    要想URL重写,肯定是会用到正则表达式的。

    这样,就成功了。

    ——————————————————————

    另外,微软还提供了一个dll文件,URLWriter.dll ,

    我们也可以使用它来直接进行URL重写。

    ①添加引用URLwriter.dll文件

    ②在web.config文件中,找到<configSections>节点,在结束标志</configSections>前添加代码

    第一步
    <configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandlers, URLRewriter">

    </configSections>

    ③在web.config文件中,还是找到<configSections>节点,在结束标志</configSections>后,添加代码

    第二步
    <RewriterConfig>
    <Rules>
    <RewriterRule>
    <LookFor>~/user/blogs.aspx</LookFor>
    <SendTo>~/user/blogs.aspx?id=$1</SendTo>
    </RewriterRule>
    </Rules>
    </RewriterConfig>

    ④找到节点<httpHandlers>,在<httpHandlers>中添加

    <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler,URLRewriter"/>
  • 相关阅读:
    jsp grid can not be used in this ('quirks') mode
    weblogic stage更改不马上生效
    shell执行class或jar
    java json字符串与对象转换
    js对象及元素复制拷贝
    js中json字符串与对象的转换及是否为空
    js window.open隐藏参数提交
    poi excel文件名或者内容中文乱码
    linux poi生成excel demo调试附调用代码
    PeekMessage与GetMessage的对比
  • 原文地址:https://www.cnblogs.com/laov/p/2342985.html
Copyright © 2011-2022 走看看