Using ASP.NET redirect without string url?
For example, now I have the following:
Page.Response.Redirect("Default.aspx", false);
Having to hard-code a string seems odd to me. The Default.aspx page is already in my project. So there is a way to do the redirect like this:
Page.Response.Redirect(Default.aspx, false);
Where Default.aspx is just a web form. I would have thought that this way it would be obvious if there was a problem like you deleted the web form but didn't update the redirects.
Is it possible? Or is there another way I should be looking at this?
I suppose I could do something with a static property in the class, but I'm wondering if there is a built in thing for this?
a source to share
Default.aspx is different from the class Default
that is automatically generated for you by the IDE. You can easily have multiple .aspx files inheriting from the same 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 deal with this is to just load the lines you are using to redirect if you do it in multiple places.
a source to share
In theory, what you suggest makes a lot of sense, however, not every website uses files to host content. For example, you can run a website from a compressed zip ( http://msdn.microsoft.com/en-us/library/aa479502.aspx ) as well as almost any other source using the virtual provider path.
However, I can see the significance of such a function. You can suggest it to the VS IDE team:
a source to share
I understand what you want to achieve and I think you could create your own method:
public string GetPageName(Type t)
{
return t.name + ".aspx";
}
And call it like this:
Response.Redirect(typeof(Default));
Problem is that it is in no way reliable. First of all, you need to make sure that this method is always called before redirection, otherwise it would be pointless. Another problem is that you don't know what Default.aspx
is in the root folder. And what about all your HttpHandlers whose path is configured in the web.config file or via the .ashx
.
Update And also consider the great point made by @womp
a source to share
If you are using forms authentication, there is a DefaultUrl parameter in the web.config file that would be built in, in idea to use.
a source to share