C# .NET - URLREWRITING

Asked By shweta on 16-Jun-10 03:49 AM
Please guide me with steps[ No link reference plzzzzzzzzz]
I am trying to implement urlrewriting but i am getting stuck wid every approach i follow.
i want to convert links like http://localhost:3358/abc--New/FurtherResult.aspx?uid=19 to a seo friendly format

As u guide plz be connected via this thread
Anand Malli replied to shweta on 16-Jun-10 05:02 AM
Hi Shweta,

I know you want steps no links, however you should check this excellent article by Scott Guthrie on URL Rewriting 

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

Let me know if you find any problem for implementation...
shweta replied to Anand Malli on 16-Jun-10 05:46 AM
Thanks for replying :)
I am following approach2 from the aricle which u have mentioned.. but  <rewriter>displays a msg that it is a invalid child element.
<configuration>
    <configSections>
    
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <!--<sectionGroup name="system.web">
        <section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter,
        ThunderMain.URLRewriter, Version=1.0.783.30976,
        Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/>-->
      <!--</sectionGroup>-->
      <section name="rewriter"  requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
      </sectionGroup>
    </sectionGroup>
    </sectionGroup>
    <!--<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />-->
    </configSections>
</configuration>
This is the way i include Intelligencia.UrlRewriter dll
shweta replied to Anand Malli on 16-Jun-10 05:52 AM
The error says: Unrecognized configuration section rewriter.
Anand Malli replied to shweta on 16-Jun-10 05:52 AM
Hi Shweta,

as you have mentioned part of your web.config file which seems pretty ok.

make sure you have both pf the following part mentioned in your web.config

<httpModules>
   <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
   </httpModules>

<modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
  </modules>

<rewriter>
   <rewrite url="~/products/(.+)" to="~/products.aspx?category=$1" />
 </rewriter>


can you please post your full web.config over here as I believe thr might be some problem with the configuration itself...?




shweta replied to Anand Malli on 16-Jun-10 06:01 AM

<?xml version="1.0"?>
<configuration>
    <configSections>
    
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
      
      <section name="rewriter"  requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
      </sectionGroup>
    </sectionGroup>
    </sectionGroup>
    <!--<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />-->
    </configSections>
  <rewriter>
    <rewrite url="~/FurtherResult/(.+).aspx" to="~/FurtherResult.aspx?uid=$1" />
  </rewriter>
    <appSettings>
      <!--Note: API Key for Commeciallocation.co.uk-->
      <!--<add key="googleApiKey" value="ABQIAAAA2oSlKiAS_wpawxiG1Z0yWxQiaETYqivIJyAIo_XLJOf3feN4VRQvHbE8LxemaiYmSHQ2sztSkgCaww"/>-->
      <!--Note: API Key for http://83.170.93.214/Commercial/ -->
      <add key="googleApiKey" value="ABQIAAAA2oSlKiAS_wpawxiG1Z0yWxTd_ZyElA9YNnyWP3x_dQY17X7vjhQJYZN_vrXpgIPoukel8ApZ9Exu3w"/>
      <add key="CustomUrl" value="http://localhost:3358/f--New/LoadCustompage.aspx?LoadCustomPage="/>
      <add key="ResultUrl" value="http://localhost:3358//FurtherResult.aspx?uid="/>
    </appSettings>
    <connectionStrings>
      <add name="ConnectionStringAllocation" connectionString="Server=f;Database=f;uid=f;pwd=f;" providerName="System.Data.SqlClient"/>
      <add name="DbAllocationConnectionString1" connectionString="Server=f;Database=f;uid=f;pwd=f;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio Express"" providerName="System.Data.SqlClient"/>
      <add name="DbAllocationConnectionString2" connectionString="Server=f;Database=f;uid=f;pwd=f;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio"" providerName="System.Data.SqlClient"/>
      <add name="comercialConnectionString" connectionString="Data Source=f;Initial Catalog=f;User ID=f;Password=f" providerName="System.Data.SqlClient"/>
      <add name="comercialConnectionString1" connectionString="Data Source=f;Initial Catalog=f;User ID=f" providerName="System.Data.SqlClient"/>
      <!--<add name="ConnectionStringAllocation" connectionString="Server=f.db.5617202.hostedresource.com;Database=f;uid=f;pwd=f;" providerName="System.Data.SqlClient"/>
    <add name="DbAllocationConnectionString1" connectionString="Server=justmovein.db.5617202.hostedresource.com;Database=f;uid=f;pwd=f;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio Express"" providerName="System.Data.SqlClient"/>
    <add name="DbAllocationConnectionString2" connectionString="Server=f.db.5617202.hostedresource.com;Database=f;uid=f;pwd=f;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio"" providerName="System.Data.SqlClient"/>
     </connectionStrings>
    <system.net>
      <mailSettings>
          <!--
      Give Email Address of Sender here
      -->
          <smtp from="no-reply@commerciallocation.co.uk" deliveryMethod="SpecifiedPickupDirectory">
              <!--
      Here give ip address,port no.,username and password of server for host,port,username and password
      -->
              <!--<network host="relay-hosting.secureserver.net" port="25" password="g" userName="g@googlemail.com" />-->
              <network host="smtp.googlemail.com" port="465" password="f" userName="no-reply@commerciallocation.co.uk"/>
          </smtp>
      </mailSettings>
    </system.net>
    <system.web>
 
   <pages>
          <controls>
              <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          </controls>
      </pages>
      <identity impersonate="true"/>
      <authentication mode="Forms">
          <forms timeout="90" name="SqlAuthCookie" defaultUrl="Registration.aspx"/>
      </authentication>
      <!--
        The <customErrors> section enables configuration
        of what to do if/when an unhandled error occurs
        during the execution of a request. Specifically,
        it enables developers to configure html error pages
        to be displayed in place of a error stack trace.
-->
      <customErrors mode="Off">
          <!--<error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />-->
      </customErrors>
      <machineKey validationKey="F71A2AD246E5FB0C55AD4F908702680DD9383C961427F0C9BBBC15A67644F793CCB3FAB9420CE70554DFE58FE161F724144CDD5591E249CF21C85F948F3BD8EE" decryptionKey="2DD434280DDC3FE6D93486851B33162BA62B8CA4A3393E1E" validation="SHA1"/>
      <membership defaultProvider="MySqlMembershipProvider">
          <providers>              <add connectionStringName="ConnectionStringAllocation" applicationName="\Commercial" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="^(?=.*\d).{5,}$" name="MySqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
          </providers>
      </membership>      <sessionState timeout="60"></sessionState>
      <compilation debug="true">
          <assemblies>
              <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
              <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
              <add assembly="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
              <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
              <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
              <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
              <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
              <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
              <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
              <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
              <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
              <add assembly="System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies>
      </compilation>
      <httpHandlers>
          <remove verb="*" path="*.asmx"/>
    <!--<add verb="*" path="*.aspx"  type="URLRewriter.RewriterFactoryHandler, URLRewriter" />-->

    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
          <add verb="POST,GET" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
      </httpHandlers>
      <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
    <!--<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />-->
    <!--<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />-->

    </httpModules>
    </system.web>
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
          <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </modules>
      <handlers>
          <remove name="WebServiceHandlerFactory-Integrated"/>
          <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
          <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </handlers>
    </system.webServer>
</configuration>

Anand Malli replied to shweta on 16-Jun-10 06:21 AM
Hi Shweta,

I have found one problem with your configuration settings.

following entry:
<section name="rewriter" 
      requirePermission="false"
      type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

keep it outside of all the "sectionGroup" tags. Better if you put this entry @ just above the </configsections> out side of the last "sectionGroup" tag

It will be look like...
   </sectionGroup>
 </sectionGroup>
 </sectionGroup>
<section name="rewriter"  requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
 </configSections>



I hope you got my point...


shweta replied to Anand Malli on 16-Jun-10 06:25 AM
I had tried that, u are right that the error gets resolved but still the url doesnt get rewritten... :(
Anand Malli replied to shweta on 16-Jun-10 06:45 AM
Hi Shweta,

I have downloaded the sample solution for URL rewriting mentioned in the article.
http://www.scottgu.com/blogposts/urlrewrite/UrlRewrite_HttpModule1.zip

It's working properly fine @ my end.

I hope u're following exactly same...
shweta replied to Anand Malli on 16-Jun-10 06:56 AM
Yes, what i dis is:
1. mentioned in the <configSections>....
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

2. <httpModules>
    
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
  </
httpModules>
   

3. </configSections>
  <rewriter>
    <rewrite url="~/FurtherResult/(.+).aspx" to="~/FurtherResult.aspx?uid=$1" />
  </rewriter>

4. copy n pasted App_Browsers folder from the sample application

5. copy n pasted FormRewriter.vb into App_code-->BLL folder
Anand Malli replied to shweta on 16-Jun-10 07:06 AM
Hi sweta,

steps you have mentioned look fine...

if possible can you plz post your code over here...
shweta replied to Anand Malli on 16-Jun-10 07:08 AM
which part of code?
Anand Malli replied to shweta on 16-Jun-10 07:17 AM

your main page markup where you are having link to other page for which you want url to be rewritten.

i.e. FurtherResult.aspx


Anand Malli replied to Anand Malli on 16-Jun-10 08:02 AM
Hi Shweta,

I did @ my end by creating new page & creating new rewrite entry in the web.config for my new page.

it's working like a charm...

I've added one link with
<li><a href="FurtherResult/19.aspx">Results</a></li>
 
& the same <rewrite> entry as yours..

it's working fine...
Anand Malli replied to shweta on 16-Jun-10 08:13 AM
Hi Shweta,

I did @ my end by creating new page & creating new rewrite entry in the web.config for my new page.

it's working like a charm...

I've added one link with
<li><a href="FurtherResult/19.aspx">Results</a></li>
 
& the same <rewrite> entry as yours..

it's working fine...
shweta replied to Anand Malli on 16-Jun-10 08:39 AM
I have not included any code for urlrewriting on it.............
shweta replied to shweta on 16-Jun-10 08:59 AM
My link is as: <a href="FurtherResult.aspx?uid=<%#Eval("PropertyId") %>" class="green_link">  <strong>more..</strong>.</a>
Zaina . replied to shweta on 16-Jun-10 09:31 AM
Hi Shweta,
You hav mentioned for code only but if you understand this from this i think you'll get more information.
Hopes it will help you. :-)
see this link - http://www.codeproject.com/KB/aspnet/urlrewriter.aspx
Thanks & Regards
Aneeta
Anand Malli replied to shweta on 16-Jun-10 09:44 AM
Sweta,
you're doing a small mistake in your code..

your link is this...

My link is as: <a href="FurtherResult.aspx?uid=<%#Eval("PropertyId") %>" class="green_link">  <strong>more..</strong>.</a>

you need to change the link for
 <a href="FurtherResult\<%#Eval("PropertyId") %>.aspx" class="green_link">  <strong>more..</strong>.</a>

then only it will work as per your config settings....

let me know the result...

shweta replied to Anand Malli on 17-Jun-10 12:37 AM

Hi,

Good Morning! The url gets rewritten now put the javascript on FurtherResult has stopped working...

The javascript added used to display the image we had clicked on in a div included on the page. Now when i click on the image, i get a message..

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

shweta replied to shweta on 17-Jun-10 12:51 AM
The images, css settings are not shown and as i was passing value via query string i now face an issue as url has been changed :(
Anand Malli replied to shweta on 17-Jun-10 01:01 AM
Shweta, you need to take care while using image url to have matching pattern similar to the one you have mentioned in the config file. then only it will find the correct path of the image file.

if possible plz post the javascript code which u're using to display image file...


shweta replied to Anand Malli on 17-Jun-10 01:07 AM

i have mentioned image path as <img src="images/slideshow-btn.gif" alt="" />

i use javascript:

    <script type="text/javascript" src="js/popup.js"></script>

apply is as <ItemTemplate>
                <span class="pL10"><a href="<%#Eval("PropertyImage") %>" rel="enlargeimage::click"
                  rev="loadarea2">
                  <asp:Image Style="border: solid 1px #787777;" ID="Image1" Height="63px" Width="85px"
                    ImageUrl='<%#NoImage(Eval("PropertyImage").ToString()) %>' runat="server" /></a></span>
              </ItemTemplate>

shweta replied to shweta on 17-Jun-10 01:08 AM
Also let me know if i change the way url looks how will i work will query strings.....................