Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom provider in the url rewrite module

I am developing on a website which is currently hosted in Azure on a VM. Now I am changing this website to be able to run it as an Azure website.

Now my problem: I am using the url rewrite module from IIS with the database provider, which works perfectly in the VM. With the database provider users of the website can create their own simple rewrite rules.

But when I upload my website as an Azure Website and I access a url that's specified in the database I get an error:

"The page cannot be displayed because an internal server error has occurred.".

This is the logging configuration I currently use:

<rewrite>
  <rules configSource="RewriteInbound_Live.config" />
  <outboundRules configSource="RewriteOutbound_Live.config" />
  <providers>
    <provider name="DB" type="DbProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
      <settings>
        <add key="ConnectionString" value="*****" />
        <add key="StoredProcedure" value="sp_GetRewrittenUrl" />
        <add key="CacheMinutesInterval" value="60" />
      </settings>
    </provider>
  </providers>
</rewrite>

I've turned on web server logging which doesn't give me any information, and I've enabled application logging which also doesn't give me any information.

My question, is it possible to use custom providers for the url rewite module in Azure, of can this be achieved in another way?

like image 412
user3782553 Avatar asked Jun 27 '14 09:06

user3782553


1 Answers

I had the same problem: DbProvider not available in azure web app. So I left most of the url rewriting stuff in the web.config untouched and moved the database-based rules to the global.asax file's Application_BeginRequest method.

For redirect rules, just use a Response.Redirect or an adequate Redirect301 implementation. On the other hand, for the rewrites, use HttpContext.Current.RewritePath.

You should add a caching mechanism in order to reduce repetitive queries to the DB:

void Application_BeginRequest(Object sender, EventArgs e)
{
    //TODO: Cache sql queries
    string requestUrl = Request.FilePath;
    using (DBConn conn = new DBConn("GetRedirectUrl"))
    {
        conn["input"] = requestUrl;
        string res = conn.ExecuteScalar()?.ToString();

        if (!string.IsNullOrEmpty(res))
        {
            Response.Redirect301(res);
        }
    }


    //TODO: Cache sql queries
    using (DBConn conn = new DBConn("GetRewrittenUrl"))
    {
        conn["input"] = requestUrl;
        string res = conn.ExecuteScalar()?.ToString();

        if (!string.IsNullOrEmpty(res))
        {
            HttpContext.Current.RewritePath(res);
        }
    }
}
like image 75
garo cm Avatar answered Sep 27 '22 20:09

garo cm