Loading ...

Problem with URL Rewriting in ASP.Net | CodeAsp.Net

Problem with URL Rewriting in ASP.Net

 /5
0 (0votes)

I have faced a problem when i m doing url rewriting in my project.I am using a repeater in which I show the Forum name and a topic post name when I click on the Forum name it redirect on the test.aspx page and when i click on the topic Postname it redirect on the test1.aspx.
In url rewriting my url rewriting format for Forum name is  www.test.com/gorupname/forumid/forumName.aspx and for topic post the format is www.test.com/gorupname/forumname/topicid/topicName.aspx. i have use the following code in web.config

      <add name="Forum" virtualUrl="~/forums/(.*)/([0-9]*)/(.*).aspx" destinationUrl="~/forums/topic.aspx?forumid=$2" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true"/>
<add name="ForumTopic" virtualUrl="~/forums/(.*)/(.*)/([0-9]*)/(.*).aspx" destinationUrl="~/forums/topicview.aspx?tid=$3" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true"/>
 and use the folloeing code in the .cs side
 protected string CreateForumUrl(object group, object name, object id)
        {
            return ReplaceChar(Convert.ToString(group)) + "/" + Convert.ToString(id) + "/" + ReplaceChar(Convert.ToString(name)) + ".aspx";
        }

        protected string CreateGroupUrl( object groupId,object groupName)
        {
            return ReplaceChar(Convert.ToString(groupId)) + "/" + Convert.ToString(groupName) + ".aspx";
        }
      
        protected string CreateTopicPostUrl(string groupName, string forumName, object topicId, object topicName)
        {
            return ReplaceChar(Convert.ToString(groupName)) + "/" + ReplaceChar(Convert.ToString(forumName)) + "/" + Convert.ToString(topicId) + "/" + ReplaceChar(Convert.ToString(topicName)) + ".aspx";
        }
      where ReplaceChar is a function to replace the special character.
following code in .aspx side
 
                <asp:Repeater ID="rptForum" runat="server" >
                    <ItemTemplate>
                   
                        <!-- start forum -->
                        <table class="simTable">
                            <tbody>
                                <tr>
                                    <td width="500px"><b><a href='<%# CreateForumUrl(Eval("GroupName"), Eval("Name"), Eval("ForumID").ToString() )  %>' title="<%# Eval("Name") %>"><%# Eval("Name") %></a></b><br /><%# LongTextTruncation(Eval("forumID"))%>
                                   
                                    </td>
                                    <td width="50px" style="text-align:center;"><%# Eval("NoOfTopics")%></td>
                                    <td width="200px"><small><%# GetTopicPosted(Eval("LastPostID"), Eval("LastUserId"))%></small></td>
                                </tr>
                            </tbody>
                        </table>
                        <!-- end forum -->
                       
                    </ItemTemplate>
                </asp:Repeater>
               
Now when I click on the the forum name then it will redirect me the test.aspx  and when I am click on the Topic PostName then it will also redirect me on the test.aspx page not on test1.aspx page.
When I analyze this the I found that it all is happening for the web.config and found that rule (which I mentained in web.config) url rewriting for forumName and topic post is simmlier so when it check the rule and got the same rule for forum name that’s why it redirect me on the same test.aspx page. To remove that I have just replace the possition of rule. And now it is working properly because in topic page there is so many “/” directory and in forum name there is only 3 directory(“/”).
<add name="ForumTopic" virtualUrl="~/forums/(.*)/(.*)/([0-9]*)/(.*).aspx" destinationUrl="~/forums/topicview.aspx?tid=$3" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true"/>


      <add name="Forum" virtualUrl="~/forums/(.*)/([0-9]*)/(.*).aspx" destinationUrl="~/forums/topic.aspx?forumid=$2" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true"/>

So always use more directory url rewriting first when some step(rules) are same

Comments (no comments yet)

  • :*
  • :*
  • :
 *

Top Posts