Tutorial :How to get without “default.aspx” url?



Question:

I implemented a following code in Global.asax file of my web application.

void Application_BeginRequest()  {        string rule = ConfigurationManager.AppSettings.Get("WwwRule");        HttpContext context = HttpContext.Current;      if (context.Request.HttpMethod != "GET" || context.Request.IsLocal)      {          return;      }        if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))      {          string url = context.Request.Url.ToString();            if (!url.Contains("://www.") && rule == "add")          {              string url = context.Request.Url.ToString().Replace("://", "://www.");              context.Response.Redirect(url);          }      }  }  

When I am running above code it works as follows

example.com redirects to www.example.com/default.aspx

www.example.com redirects to www.example.com

http://www.example.com/ redirects to http://www.example.com/

last two conditions works very well. But the first condition did'nt works well because its adding "default.aspx" in the URL which I am not intrested in.

Can anyone please tell me how to make it as below

example.com should redirects to http://www.example.com

Thanks


Solution:1

Most likely the Request.Url is appending default.aspx because that's the page actually being served at the time (IIS makes this transparent to you because it's one of the default pages).

When you make your new URL that you're going to redirect, add another .Replace("/default.aspx", "") to the end of it. So...

string url = context.Request.Url.ToString().Replace("://", "://www.").Replace("/default.aspx", "");  


Solution:2

Actually, the /default.aspx is added before the request reaches the BeginRequest event. If you want to remove it, you have to actually remove it:

void Application_BeginRequest() {      string rule = ConfigurationManager.AppSettings.Get("WwwRule");        HttpContext context = HttpContext.Current;      if (context.Request.HttpMethod != "GET" || context.Request.IsLocal) {          return;      }        if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase)) {          string url = context.Request.Url.ToString();            if (!url.Contains("://www.") && rule == "add") {              url = url.Replace("://", "://www.");              if (url.EndsWith("/default.aspx", StringComparison.OrdinalIgnoreCase) {                 url = url.Substring(0, url.Length - 13);              }              context.Response.Redirect(url);          }      }  }  


Solution:3

change your webconfig with below code:it solve my same problem.

<?xml version="1.0"?>  <configuration>      <system.webServer>          <rewrite>              <rules>                  <rule name="default.aspx Redirect" stopProcessing="true">                      <match url="^(.*\/)*default\.aspx$" />                      <conditions logicalGrouping="MatchAll">                          <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />                      </conditions>                      <action type="Redirect" url="{R:1}" redirectType="Permanent"/>                  </rule>              </rules>          </rewrite>      </system.webServer>  </configuration>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »