ASP.NET 301 Redirect

Using PHP, I can setup a 301 redirect like this:

<?php
header("Location: http://www.google.com/",TRUE,301);
?>

How can I do this with ASP.NET?  My code below does NOT work.


<%@ 

protected void Page_PreInit(object sender, EventArgs e)
{
    Response.StatusCode = 301;
    Response.StatusDescription = "Moved Permanently";
    Response.AddHeader("Location", "http://www.google.com/");
}

%>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
lazyberezovskyCommented:
Add using at the top of codebehind file :)

using System;

Open in new window

0
 
VenabiliCommented:
Whatever happened in your two questions (edited the other one while I was posting?)
anyway

Had you tried with
Response.Status = "301 Moved Permanently";
instead of the two settings for the status that you have now?

see this also: http://professionalaspnet.com/archive/2006/02/14/redirect301.aspx
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
lazyberezovskyCommented:
Even more simple (use RedirectLocation instead of using the AppendHeader method):

Response.Status = "301 Moved Permanently";
Response.RedirectLocation = "http://www.google.com/";

Open in new window

0
 
hankknightAuthor Commented:
Thank you, but I still don't understand.  I may be missing the basics of ASP.NET because I have never used it before.

I could not get any of your ideas to work.  Any idea what I am missing?
########### From hielo's post ########### 

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.google.com/"
%> 



########### From hielo's post ########### 

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.google.com");
}
</script> 


########### From lazyberezovsky's post ########### 

<%@  
Response.Status = "301 Moved Permanently"; 
Response.RedirectLocation = "http://www.google.com/";
%>

Open in new window

0
 
lazyberezovskyCommented:
Add my code to codebehind class.
Make sure page directive has AutoEventWireup set to true.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Status = "301 Moved Permanently";
        Response.RedirectLocation = "http://www.google.com/";
    }
}

Open in new window

0
 
hankknightAuthor Commented:
Thank you, lazyberezovsky.

I am still getting an error.  Is there a way to know what I am doing wrong?
Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0246: The type or namespace name 'EventArgs' could not be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 1:  public partial class _Default : System.Web.UI.Page 
Line 2:  { 
Line 3:      protected void Page_Load(object sender, EventArgs e) 
Line 4:      { 
Line 5:          Response.Status = "301 Moved Permanently"; 

Source File: e:\web\myusername\CMS\Default.aspx.cs    Line: 3 


Show Detailed Compiler Output:


C:\Windows\SysWOW64\inetsrv> "C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe" /t:library /utf8output /R:"C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Windows\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Windows\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Design\2.0.0.0__b03f5f7f11d50a3a\System.Design.dll" /R:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_global.asax.wqaelvjt.dll" /R:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_Code.xtek2doa.dll" /R:"C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /out:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_Web_3z3qu3zi.dll" /debug- /optimize+ /w:4 /nowarn:1659;1699;1701 /warnaserror-  "C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_Web_3z3qu3zi.0.cs" "C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_Web_3z3qu3zi.1.cs" "C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ad4a3313\a8666223\App_Web_3z3qu3zi.2.cs"


Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

e:\web\myusername\CMS\Default.aspx.cs(3,45): error CS0246: The type or namespace name 'EventArgs' could not be found (are you missing a using directive or an assembly reference?)


Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016

Open in new window

0
 
hankknightAuthor Commented:
Thank you.  The working code is below:
########### Default.aspx ###########  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


########### Default.aspx.cs ###########  

using System;
public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        Response.Status = "301 Moved Permanently"; 
        Response.RedirectLocation = "http://www.google.com/"; 
    } 
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.