Removing default.aspx from the url in ASP.NET 2005 - asp.net

I have a app that is setup on IIS 6.0. We are having trouble with Search Engine optimization with the default.aspx page.
For Example when I type www.xxxxxx.com/default.aspx it should redirect to www.xxxxxx.com.
Can anyone please help me with this problem?

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (HttpContext.Current.Request.RawUrl == "/default.aspx")
{
Response.StatusCode = 301;
Response.Status = "301 Moved Permanently";
Response.RedirectLocation = "/";
Response.End();
}
}
}
}

Set the default document in IIS under the Documents tab. Once in the Documents tab, check "Enable default content page" and set Default.aspx as the first item (or only) in the list.

Related

How to simulate MVC routing in WebForm ASP.Net?

You all have seen how MVC minifies URL by default in form of url: "{controller}/{action}/{id}". It's done in RouteConfig.cs.
I'm looking for a way so that a webform URL like mywebsite.com/Page/Default.aspx?id=100&Browser=ff changes to mywebsite.com/Page/Default/100?Browser=ff, It should be done in Globa.ascx.
There are some posts in StackOverFlow website which instructs how to redirect a reserved URL to a certain page, it's obvious that my question is something else, I'm looking for a way to offer a pattern in Global.ascx.
At the solution explorer, under your project, add a new ASP.NET item "Global.asax"
Add the using statement:
using System.Web.Routing;
At the Application_Start event, type in your routing URL, for example:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("default1", "Page/Default", "~/Page/Default.aspx");
RouteTable.Routes.MapPageRoute("default2", "Page/Default/{controller}/{action}/{id}", "~/Page/Default.aspx");
}
Then, at the page load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string controller = RouteData.Values["controller"] + "";
string action = RouteData.Values["action"] + "";
string id = RouteData.Values["id"] + "";
}
}

ASP.Net Dynamically switch Master Pages

Never needed to do this before but is it possible to dynamically set/change which master page a page is using? Have an old asp.net web forms project which I have created a new bootstrap template for but the boss wants to give people the opportunity to switch on the new one instead of forcing it upon them.
I would recommend you to create a BasePage class than write this method in that class and inherit all of your pages from this class whose master page can be changed dynamically.
public class BasePage: System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
}
And then in your page inherit page from BasePage like this
public partial class _Default:BasePage
The master page is changed only in preint event
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (conduction1)
this.Page.MasterPageFile = "~/MasterPage.master";
else
this.Page.MasterPageFile = "~/Master.master";
}
catch (Exception ex)
{
}
}
or
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
page.MasterPageFile = "string location of masterpage";
}

How to 301 redirect to remove ending / on asp.net

In order to prevent duplicity on my category pages I want to redirect those ending with / to without:
from - www.domain.com/category/
to - www.domain.com/category
I'm using standard web forms.
Please advise,
Thanks.
In global.asax file add following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().EndsWith("/category/"))
{
string sNewPage = Request.Url.ToString().ToLower().TrimEnd('/');
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", sNewPage);
Response.End();
}
}

Make an asp .net web app offline

Basically a web app that we distribute to clients, one of whom will be trialling it so I need to be able to switch it off at a certain point. Don't want to put the end date in the web.config in case they work out they can change it, I was thinking of putting something in the global.asax with a hard coded date, but then I'm not sure how I can 'turn off' the app. I was thinking of checking the date in the Authenticate Request part and simply redirecting to a page that says your trial is finished (or something similar), but is there a better way?
You can do that on global.asax as:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if(DateTime.UtcNow > cTheTimeLimitDate)
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.Write("...message to show...");
HttpContext.Current.Response.StatusCode = 403;
HttpContext.Current.Response.End();
return ;
}
}
this is safer than place it on web.config, but nothing is safe enough. Its even better there to redirect them to a page, or not show them a message, or what ever you think.
For make redirect to a page you also need to check if the call if for a page, and the code will be as:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string cTheFile = HttpContext.Current.Request.Path;
string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
{
// and here is the time limit.
if(DateTime.UtcNow > cTheTimeLimitDate)
{
// make here the redirect
HttpContext.Current.Response.End();
return ;
}
}
}
To makes it even harder, you can make a custom BasePage that all page come from it (and not from System.Web.UI.Page) and you place there the limit on the render of the page - or show a message on top of every page render, that the time is ends.
public abstract class BasePage : System.Web.UI.Page
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if(DateTime.UtcNow > cTheTimeLimitDate)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// render page inside the buffer
base.Render(htmlWriter);
string html = stringWriter.ToString();
writer.Write("<h1>This evaluation is expired</h1><br><br>" + html);
}
else
{
base.Render(writer);
}
}
}
Just add the app_offline.htm and you can even create a nice message for your users. Also it's very easy to put the site back online, just remove or rename the app_offline.htm.
http://weblogs.asp.net/dotnetstories/archive/2011/09/24/take-an-asp-net-application-offline.aspx

Garbled error page output using Gzip in ASP.NET / IIS7

I've implemented Rick Strahl's GZipEncodePage method on my site and it works great for the site itself. However, when my code throws an exception the "Server Error" page looks something like this:
(source: x01.co.uk)
I've tried to hooking into Application_Error in an effort to remove the GZip headers but to no avail. How I can reverse the GZipping on error?
I'm understand that this question is really outdated.
On Application_Error remove Filters from Response, like this
protected void Application_Error(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Response.Filter = null;
}
Hope this helps anybody.
In my case I put this in the my basepage class like so:
public class BasePage : System.Web.UI.Page
{
protected override void OnError(EventArgs e)
{
base.OnError(e);
System.Web.HttpContext context = System.Web.HttpContext.Current;
if (context != null && context.Response.Filter != null)
context.Response.Filter = null;
}
}

Resources