search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

View Other Visual Studio .NET Posts   Ask New Question 
UrlRewriting through HTTP Module
vijay kumar posted at Friday, September 12, 2008 8:06 AM

Hello All !!!

I am using HTTP Module for url rewriting... URL rewriting is working fine however as soon as any page event(e.g. Button Click) occures the original URL get desplayed.

        Will you please suggest how to handle this.

 

Thank You

Regards

Vijay


 
  use this
Web star replied to vijay kumar at Friday, September 12, 2008 8:44 AM

using System;

 

namespace Rewrite.NET {

       public class Rewrite : System.Web.IHttpModule {

 

              /// <summary>

              /// Init is required from the IHttpModule interface

              /// </summary>

              /// <param name="Appl"></param>

              public void Init(System.Web.HttpApplication Appl) {

                     //make sure to wire up to BeginRequest

                     Appl.BeginRequest+=new System.EventHandler(Rewrite_BeginRequest);

              }

 

              /// <summary>

              /// Dispose is required from the IHttpModule interface

              /// </summary>

              public void Dispose() {

                     //make sure you clean up after yourself

              }

 

              /// <summary>

              /// To handle the starting of the incoming request

              /// </summary>

              /// <param name="sender"></param>

              /// <param name="args"></param>

              public void Rewrite_BeginRequest(object sender, System.EventArgs args) {

                     //process rules here

              }

 

       }

}

http://www.15seconds.com/Issue/030522.htm

 
UrlRewriting through HTTP Module
Sujit Patil replied to vijay kumar at Friday, September 12, 2008 8:50 AM

May be something is going wrong somewhere.

See this information and code for the same;

HttpModule to Perform URL Rewriting

An alternative approach to the above Request.PathInfo technique would be to take advantage of the HttpContext.RewritePath() method that ASP.NET provides.  This method allows a developer to dynamically rewrite the processing path of an incoming URL, and for ASP.NET to then continue executing the request using the newly re-written path.

For example, we could choose to expose the following URLs to the public:

http://www.store.com/products/Books.aspx
http://www.store.com/products/DVDs.aspx
http://www.store.com/products/CDs.aspx

This looks to the outside world like there are three separate pages on the site (and will look great to a search crawler).  By using the HttpContext.RewritePath() method we can dynamically re-write the incoming URLs when they first reach the server to instead call a single Products.aspx page that takes the category name as a Querystring or PathInfo parameter instead.  For example, we could use an an Application_BeginRequest event in Global.asax like so to do this:

    void Application_BeginRequest(object sender, EventArgs e) {

        
string fullOrigionalpath Request.Url.ToString();
        
        if 
(fullOrigionalpath.Contains("/Products/Books.aspx")) {
            Context.RewritePath(
"/Products.aspx?Category=Books");
        
}
        
else if (fullOrigionalpath.Contains("/Products/DVDs.aspx")) {
            Context.RewritePath(
"/Products.aspx?Category=DVDs");
        
}
    } 

The downside of manually writing code like above is that it can be tedious and error prone.  Rather than do it yourself, I'd recommend using one of the already built HttpModules available on the web for free to perform this work for you.  Here a few free ones that you can download and use today:

These modules allow you to declaratively express matching rules within your application's web.config file.  For example, to use the UrlRewriter.Net module within your application's web.config file to map the above URLs to a single Products.aspx page, we could simply add this web.config file to our application (no code is required):

<?xml version="1.0"?>

<configuration>

  
<configSections>
    
<section name="rewriter"  
             requirePermission
="false" 
             type
="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </
configSections>
  
  
<system.web>
      
    
<httpModules>
      
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
    </
httpModules>
    
  
</system.web>

  
<rewriter>
    
<rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />
    <
rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />
    <
rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />
  </
rewriter>  
  
</configuration> 

The HttpModule URL rewriters above also add support for regular expression and URL pattern matching (to avoid you having to hard-code every URL in your web.config file).  So instead of hard-coding the category list, you could re-write the rules like below to dynamically pull the category from the URL for any "/products/[category].aspx" combination:

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

This makes your code much cleaner and super extensible.

Sample Download: A sample application that I've built that shows using this technique with the UrlRewriter.Net module can be downloaded here

What is nice about this sample and technique is that no server configuration changes are required in order to deploy an ASP.NET application using this approach.  It will also work fine in a medium trust shared hosting environment (just ftp/xcopy to the remote server and you are good to go - no installation required).

Approach 3: Using an HttpModule to Perform Extension-Less URL Rewriting with IIS7

The above HttpModule approach works great for scenarios where the URL you are re-writing has a .aspx extension, or another file extension that is configured to be processed by ASP.NET.  When you do this no custom server configuration is required - you can just copy your web application up to a remote server and it will work fine.

There are times, though, when you want the URL to re-write to either have a non-ASP.NET file extension (for example: .jpg, .gif, or .htm) or no file-extension at all.  For example, we might want to expose these URLs as our public catalog pages (note they have no .aspx extension):

http://www.store.com/products/Books
http://www.store.com/products/DVDs
http://www.store.com/products/CDs

With IIS5 and IIS6, processing the above URLs using ASP.NET is not super easy.  IIS 5/6 makes it hard to perform URL rewriting on these types of URLs within ISAPI Extensions (which is how ASP.NET is implemented). Instead you need to perform the rewriting earlier in the IIS request pipeline using an ISAPI Filter.  I'll show how to-do this on IIS5/6 in the Approach 4 section below.

The good news, though, is that IIS 7.0 makes handling these types of scenarios super easy.  You can now have an HttpModule execute anywhere within the IIS request pipeline - which means you can use the URLRewriter module above to process and rewrite extension-less URLs (or even URLs with a .asp, .php, or .jsp extension).  Below is how you would configure this with IIS7:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

  
<configSections>
    
<section name="rewriter" 
             requirePermission
="false" 
             type
="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </
configSections>
  
  
<system.web>
      
    
<httpModules>
      
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
    </
httpModules>
    
  
</system.web>

  
<system.webServer>

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

    
<validation validateIntegratedModeConfiguration="false" />

  </
system.webServer>

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

Note the "runAllManagedModulesForAllRequests" attribute that is set to true on the <modules> section within <system.webServer>.  This will ensure that the UrlRewriter.Net module from Intelligencia, which was written before IIS7 shipped, will be called and have a chance to re-write all URL requests to the server (including for folders).  What is really cool about the above web.config file is that:

1) It will work on any IIS 7.0 machine.  You don't need an administrator to enable anything on the remote host.  It will also work in medium trust shared hosting scenarios.

2) Because I've configured the UrlRewriter in both the <httpModules> and IIS7 <modules> section, I can use the same URL Rewriting rules for both the built-in VS web-server (aka Cassini) as well as on IIS7.  Both fully support extension-less URLRewriting.  This makes testing and development really easy.

IIS 7.0 server will ship later this year as part of Windows Longhorn Server, and will support a go-live license with the Beta3 release in a few weeks.  Because of all the new hosting features that have been added to IIS7, we expect hosters to start aggressively offering IIS7 accounts relatively quickly - which means you should be able to start to take advantage of the above extension-less rewriting support soon.  We'll also be shipping a Microsoft supported URL-Rewriting module in the IIS7 RTM timeframe that will be available for free as well that you'll be able to use on IIS7, and which will provide nice support for advanced re-writing scenarios for all content on your web-server.

Sample Download: A sample application that I've built that shows using this extension-less URL technique with IIS7 and the UrlRewriter.Net module can be downloaded here

http://www.codeproject.com/KB/aspnet/dnn2url_rewrite.aspx

Best Luck!!!!!!!!!!!!!!!!!
Sujit.

 
  See this code also
Sujit Patil replied to vijay kumar at Friday, September 12, 2008 8:51 AM

HTTP module is any class that implements IHttpModule interface that contains two simple methods.

public void Init(HttpApplication application) {}
public void Dispose() {)

The first method is called, when the Http Module is initialized. There is a parameter with currenct HttpApplication refference that is passed to this Init method. You can hook to application events using delegate method. First we will hook to Application BeginRequest event, that is fired before request is handled by Http handler (in our case it handled .aspx pages).

   
public void Init(HttpApplication application) {
   application.BeginRequest += new EventHandler(this.Application_BeginRequest);
   application.PreRequestHandlerExecute += 
       new EventHandler(Application_PreRequestHandlerExecute);
}
private void Application_BeginRequest(Object source, EventArgs e)
{
   // rewrite URL to physical path...
   HttpApplication application = (source as HttpApplication);
   if (application != null) {
      HttpContext context = application.Context;
      if (context.Request.RawUrl.IndexOf("rew") > 0)
      {
          context.Items[originalRequestUrlTag] = context.Request.RawUrl;
          context.RewritePath("~/Rewrited.aspx");
      }
   }
}

You will have to substitute Application_BeginRequest method body with your own rewrite logic. In this sample all request that contain string "rew" in URL are rewrited to page ~/Rewrited.aspx. We are also hooking PreRequestHandlerExecute event. This event is called just before control is taken by Http handler. We will check if the page will be handled by System.Web.UI.Page handler and if so, we will hook PagePreInit event where we rewrite back original URL (it's very important otherwise form postback would not work correctly).

private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpApplication application = sender as HttpApplication;
    System.Web.UI.Page page = application.Context.CurrentHandler as System.Web.UI.Page;
    if (page != null)
    {
        page.PreInit += new EventHandler(Application_PagePreInit);
    }
}
static private void Application_PagePreInit(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;

    if (context.Items.Contains(originalRequestUrlTag))
    {
        // rewrite back to request Url
        context.RewritePath(context.Items[originalRequestUrlTag] as string);
    }
}

This solution is completely transparent and implemented externally using Http module. The last thig we will have to do is register our new handler in web.config file.

<system.web>
  <httpModules>
    <add name="UrlRewriteModule" type="Core.Rewrite.UrlRewriteModule"/>
  </httpModules>
</system.web>
Best Luck!!!!!!!!!!!!!!!!!
Sujit.
 
  UrlRewriting through HTTP Module
Raj Cool... replied to vijay kumar at Friday, September 12, 2008 9:42 AM

Hi,

Use below code and put your logic:

using System; 

namespace Rewrite.NET {

       public class Rewrite : System.Web.IHttpModule {

         public void Init(System.Web.HttpApplication Appl) {

         Appl.BeginRequest+=new System.EventHandler(Rewrite_BeginRequest);

         }

        public void Dispose()

       {

       }

      public void Rewrite_BeginRequest(object sender, System.EventArgs args)

     {

     } 

}

-Paresh

 
  URL Rewriting HttpModule
Binny ch replied to vijay kumar at Friday, September 12, 2008 12:50 PM
Here's a skeletal version of my own system's URL rewriting module, showing both rewrites:
    public class FriendlyUrlModule : IHttpModule

{
private const string OriginalUrlKey = "CharlieFriendlyUrlModuleRecordOriginalUrl";

// Record the original, friendly URL. This value is used later in the Pipeline.
private void BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;

string originalUrl = // code to get original URL from HttpContext.Current.Request
context.Items[OriginalUrlKey] = originalUrl;
}

// Here we rewrite path to handler
private void AuthorizeRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;

string rewrittenUrl = // code to determine the destination handler
context.RewritePath(rewrittenUrl);
}

// Here we rewrite the Path of the HttpContext back to the originally requested URL
private void PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;

string originalUrl = (string)context.Items[OriginalUrlKey];
context.RewritePath(originalUrl);
}
}
 
Go through thius link:
will it help:
http://forums.asp.net/p/858605/858605.aspx
 
Go through this link:
Binny ch replied to vijay kumar at Friday, September 12, 2008 12:52 PM

http://msdn.microsoft.com/en-us/library/ms972974.aspx

http://www.codeproject.com/KB/aspnet/dnn2url_rewrite.aspx