天天看點

ASP.NET 自定義URL重寫

一.功能說明:

可以解決類似 http://****/news 情形,url路徑支援正則比對。

二.操作步驟:

1.增加url重寫子產品:

using system;

using system.io;

using system.text.regularexpressions;

using system.web;

using system.xml;

/// <summary>

/// url重寫module

/// </summary>

public class urlrewritemodule : ihttpmodule

{

    #region ihttpmodule members

    public virtual void init(httpapplication context)

    {

        context.beginrequest += applicationbeginrequest;

    } 

    public virtual void dispose()

    }

    #endregion

    public bool isexcludedpath(string relurl)

        string fileext = path.getextension(relurl);

        if (!string.isnullorempty(fileext)

            && (fileext.tolower() == ".axd" ||

            fileext.tolower() == ".jpg" ||

            fileext.tolower() == ".png" ||

            fileext.tolower() == ".gif" ||

            fileext.tolower() == ".swf" ||

            fileext.tolower() == ".bmp"

            ))

        {

            return true;

        }

        return false;

    /// <summary>

    /// 

    /// </summary>

    /// <param name="source"></param>

    /// <param name="e"></param>

    public void applicationbeginrequest(object source, eventargs e)

        var application = (httpapplication)source;

        httpcontext context = application.context;

        try

            string path = context.request.path;

            string file = path.getfilename(path);

            if (isexcludedpath(path))

            {

                return;

            }

            if (file != null && httpcontext.current != null)

                string rewriteconfig = httpcontext.current.server.mappath("~/config/rewriterconfig.config");

                if (file.exists(rewriteconfig))

                {

                    var xml = new xmldocument();

                    xml.load(rewriteconfig);

                    xmlnodelist rules = xml.selectnodes("rewriterconfig/rules/rewriterrule");

                    if (rules != null)

                    {

                        foreach (xmlnode rule in rules)

                        {

                            string lookfor = "";

                            string sendto = "";

                            xmlnode lookfornode = rule.selectsinglenode("lookfor");

                            if (lookfornode != null)

                            {

                                lookfor = lookfornode.innertext;

                            }

                            xmlnode sendtonode = rule.selectsinglenode("sendto");

                            if (sendtonode != null)

                                sendto = sendtonode.innertext;

                            if (!string.isnullorempty(lookfor) && !string.isnullorempty(sendto))

                                string regerule = regex.escape(lookfor);

                                var regex = new regex("^(?i)" + regerule + "$", regexoptions.compiled);

                                //比對無字尾時路徑

                                if (string.isnullorempty(file))

                                {

                                    if (context.request.applicationpath != null)

                                    {

                                        var subpath = path.substring(context.request.applicationpath.length).trimstart('/').trimend('/');

                                        if (regex.match(subpath).success)

                                        {

                                            context.rewritepath(path.combine(context.request.applicationpath, sendto));

                                            break;

                                        }

                                    }

                                }

                                else

                                    if (regex.match(file).success)

                                        context.rewritepath(sendto);

                                        break;

                        }

                    }

                }

        catch (exception ex)

            context.response.clear();

            context.response.write(ex.message);

            context.response.end();

}

2.增加url重寫配置,放到網站根目錄下config檔案夾下:~/config/rewriterconfig.config

<?xml version="1.0"?>

<rewriterconfig>

    <rules>

        <rewriterrule>

            <lookfor>floor</lookfor>

            <sendto>index_floor.html</sendto>

        </rewriterrule>

            <lookfor>door</lookfor>

            <sendto>about/index_292.html</sendto>

            <lookfor>kolani</lookfor>

            <sendto>index_kolani.html</sendto>

            <lookfor>nature</lookfor>

            <sendto>index_nature.html</sendto>

            <lookfor>mobile</lookfor>

            <sendto>index_mobile.html</sendto>

    </rules>

</rewriterconfig>

3.在webconfig裡注冊httpmodule;注意有2個地方需要處理

內建模式下:

<system.webserver>

    <modules>

        <!--url重寫-->

         <add name="urlrewritemodule" type="urlrewritemodule" />

經典模式:在config/httpmodules.config裡

<httpmodules> 

    <!--url重寫-->

    <add name="urlrewritemodule" type="urlrewritemodule" />

4.如果是無字尾路徑,比如/news,iis6時需在iis上增加通配符配置;

ASP.NET 自定義URL重寫

實際使用過程中,可能需要您的比對規則進行相應的修改,代碼僅供參考。

繼續閱讀