Rewrite URL in ASP.NET 3.5 and IIS 7 using the HTTP module

I am developing an application in ASP.NET 3.5 and IIS 7. I wrote an HTTP module to execute URL Rewrite, for example, I want to rewrite the username on the account ID "~ / Profiles / profile.aspx? AccountID =" + account.AccountID. ToString ();

See below:

using System; using System.Collections.Generic; using System.Data; using System.Configuration; using System.IO; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;

public class UrlRewrite : IHttpModule
{
    private AccountRepository _accountRepository;
    private WebContext _webContext;

    public UrlRewrite()
    {
        _accountRepository = new AccountRepository();
        _webContext = new WebContext();
    }

    public void Init(HttpApplication application)
    {
        // Register event handler.
        application.PostResolveRequestCache +=
            (new EventHandler(this.Application_OnAfterProcess));
    }

    public void Dispose()
    {
    }

    private void Application_OnAfterProcess(object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;

        string[] extensionsToExclude = { ".axd", ".jpg", ".gif", ".png", ".xml", ".config", ".css", ".js", ".htm", ".html" };
        foreach (string s in extensionsToExclude)
        {
            if (application.Request.PhysicalPath.ToLower().Contains(s))
                return;
        }

        if (!System.IO.File.Exists(application.Request.PhysicalPath))
        {
            if (application.Request.PhysicalPath.ToLower().Contains("blogs"))
            {

            }
            else if (application.Request.PhysicalPath.ToLower().Contains("forums"))
            {

            }
            else
            {

                string username = application.Request.Path.Replace("/", "");

                Account account = _accountRepository.GetAccountByUsername(username);

                if (account != null)
                {
                    string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString();
                    context.Response.Redirect(UserURL);
                }
                else
                {
                    context.Response.Redirect("~/PageNotFound.aspx");
                }
            }
        }
    }
}

, web.config, , , web.config . -, , . , , ? IIS?

.

+3
2

, IIS7 Classic Integrated Pipeline Mode. Integrated Pipeline :

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" />
  </modules>
</system.webServer>

, , , , IIS7 Classic/Integrated IIS5/6 ( , ), - , IIS, :

<system.web>
  <httpModules>
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" />
  </httpModules>
</system.web>
<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" />
  </modules>
</system.webServer>

runAllManagedModulesForAllRequest = "true", , HttpModule , .aspx,.ashx,.asmx .., .NET framework.

, URL- ( ), context.RewritePath(string) .

, , application.Request.Path "" , , , :

http://www.domain.com/robertp

:

http://www.domain.com/Profiles/profile.aspx?AccountID=59

context.Response.Redirect(), context.RewritePath() :

string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString();
context.RewritePath(UserURL);

... !!

, URL-, , profiles.aspx?AccountID=59, robertp .

, IIS7, - . Dev- IIS6 IIS5, , , , , robertp , HttpModule , , .NET ISAPI.

, .

+6

URL- Fusion URL Rewriter, Tom .

http://urlrewriter.codeplex.com

, , .

MF , , , .

0

Source: https://habr.com/ru/post/1772083/


All Articles