西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)ASP.NET → Asp.NET中Url重寫控件UrlRewriter使用實例

Asp.NET中Url重寫控件UrlRewriter使用實例

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2012/5/12 0:07:05字體大小:A-A+

作者:佚名點擊:126次評論:0次標簽: UrlRewriter

  • 類型:服務器區(qū)大。19KB語言:中文 評分:5.0
  • 標簽:
立即下載

1、  添加 UrlRewriter.dll 到 bin目錄中

2、configSections 節(jié)點中添加

<configSections>
    <section name="ReplaceUrl" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
</configSections>

3、httpModules節(jié)點中添加

 <httpModules>
      <add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule"/>
</httpModules>

4、aspx頁面代碼

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>UELREWRITE</title>
    <script src="JS/jquery-1.7.1.min.js" type="text/javascript"></script>
    <link href="CSS/CSS.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript">

        $(function () {
            $.ajax({
                type: "POST",
                url: "server.action?v=" + (new Date()).valueOf(),
                cache: false,
                dataType: "html",
                success: function (data) {
                    $(".i_box_6 img").attr("src", data);
                },
                error: function () {
                    alert("oh...oh...oh...");
                }
            });
        });
    </script>
</head>
<body>
    <div class="title">
        <span>Start</span>
    </div>
    <div class="box">
        <a href="<%= ResolveUrl("index.html") %>" target="_blank">
            <div class="i_box_1">
                <p>
                    For:Index.html</p>
                <p>
                    To:default.aspx</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_2">
                <p>
                    NONE</p>
                <p>
                    UELREWRITE</p>
            </div>
        </a><a href="<%= ResolveUrl("about.php") %>" target="_blank">
            <div class="i_box_3">
                <p>
                    For:about.php</p>
                <p>
                    To:Pages/Default.aspx</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_4">
                <p>
                    UELREWRITE</p>
                <p>
                    NONE</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_5">
                <p>
                    NONE</p>
                <p>
                    UELREWRITE</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_6">
                <img />
                <p>
                    UELREWRITE</p>
                <p>
                    NONE</p>
            </div>
        </a><a href="<%= ResolveUrl("123321/20120507.html") %>" target="_blank">
            <div class="i_box_7">
                <p>
                    For:123321/20120507.html</p>
                <p>
                    To:Pages/page2.aspx?ID=123321&amp;date=20120507</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_8">
                <p>
                    NONE</p>
                <p>
                    UELREWRITE</p>
            </div>
        </a><a href="javascript:;">
            <div class="i_box_9">
                <p>
                    UELREWRITE</p>
                <p>
                    NONE</p>
            </div>
        </a><a href="<%= ResolveUrl("news/123321") %>" target="_blank">
            <div class="i_box_10">
                <p>
                    For:news/123321</p>
                <p>
                    To:Pages/page2.aspx?ID=123321</p>
            </div>
        </a>
    </div>
</body>
</html>

 5、要實現(xiàn)url重寫,需要在web.config  configSections節(jié)點中設(shè)置url的映射規(guī)則

 <ReplaceUrl>
    <urls>
      <add virtualUrl="~/server.action" destinationUrl="~/hander.ashx"/>
      <add virtualUrl="~/index.html" destinationUrl="~/Default.aspx"/>
      <add virtualUrl="~/about.php" destinationUrl="~/Pages/Default.aspx"/>
      <add virtualUrl="~/news/(.+)" destinationUrl="~/Pages/page2.aspx?ID=$1"/>
      <add virtualUrl="~/(.*)/(.*).html" destinationUrl="~/Pages/page2.aspx?ID=$1&amp;date=$2"/>
    </urls>
  </ReplaceUrl>

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)