views:

88

answers:

4

For instance, what I have right now is the following:

Page.Response.Redirect("Default.aspx", false);

Needing to hardcode the string just seems odd to me. The Default.aspx page is already in my project. So is there a way to do the redirect something like the following:

Page.Response.Redirect(Default.aspx, false);

Where Default.aspx is just the web form. I'd think that this way it would be obvious if there was a problem such as you deleted the web form but didn't update the redirects.

Is this possible? Or is there another way entirely I should be looking at this?

I suppose I could do something with a static property on the class, but I am wondering if there is a built in thing for this?

+1  A: 

In theory what you suggest makes a lot of sense, however not every website uses files to host content. You can actually run a website out of compressed zip (http://msdn.microsoft.com/en-us/library/aa479502.aspx) for example as well as just about any other type of source using virtual path providers.

Still, I can see the value of a feature like this. You may want to suggest it to the VS IDE team:

http://connect.microsoft.com/VisualStudio

Nissan Fan
+2  A: 

Default.aspx is different from the class Default, that is generated automatically for you by the IDE. You can easily have multiple .aspx files inheriting from the same Page class.

This is why there is no built-in mechanism for this - there is no guarantee of a 1 to 1 relationship between markup pages and code classes.

Probably the best way to manage this is to simply resource the strings that you are using for redirection if you're doing it in multiple places.

womp
A: 

I understand what you want to achieve, and I guess you could create your own method:

public string GetPageName(Type t)
{
     return t.name + ".aspx";
}

And call it like this:

Response.Redirect(typeof(Default));

The problem is that this is in no way foolproof. First of all, you need to make sure that this method is always called before redirecting, otherwise it would be pointless. Another problem is that you don't know that Default.aspx is in the root folder. And what about all your HttpHandlers, the path of which is configured in web.config or through an .ashx file.

Update And also consider the great point made by @womp

klausbyskov
A: 

If you are using Forms Authentication, there is a setting in the web.config for a DefaultUrl that would be a built-in idea to use.

JB King