Can I programatically find the url of a web form based on the class?
If I have a web form in e.g. ~/Page1开发者_如何转开发.aspx, that implements the Page1 class defined in the code behind file, is there any way that I programatically can determine the URL (~/Page1.asp) based on the type; something like
string url = GetUrlOfWebForm(typeof(Page1));
Or perhaps from an instance of the class
string url = GetUrlOfWebForm(new Page1());
So any code that links to the page will be updated if the page is moved/renamed, or I get a compile time error if the page is removed.
I'd be more tempted to look at the Request.Url to determine where I was in the site structure, it'll give you the URL that the browser requested.
The other approach is to make sure that all the links on your page for your site are relative links which eliminates the need to alter the links programmatically.
If all of your Pages are in the same namespace and the namespaces properly reflect the folder structure, the relative URL for a given Page can be determined by manipulating the namespace string. If there are any Pages that don't follow the pattern they can be handled on a case-by-case basis. One option for doing this is making all of your Pages inherit from a base class that contains a RelativeUrl
property that can be overridden for individual pages.
private const string PAGES_PREFIX = "Web.Pages";
public string ResolveUrl<T>() where T : Page
{
string pagePath = typeof(T).FullName;
if (pagePath.StartsWith(PAGES_PREFIX))
{
return string.Format("~/Pages/{0}.aspx", pagePath.Substring(PAGES_PREFIX.Length).Replace('.', '/'));
}
Debug.Fail(string.Format("Unable to resolve url for {0}", pagePath));
return null;
}
This solution will need to be modified to match your namespaces, but hopefully you get the idea.
That means you want to know the page URL without or before opening it, i believe that this is not possible automatically as you may think.
You will need to construct the URL string your self, because how the page class will know where its page will open for example:
http://localhost/Page1.aspx
http://website/Page1.aspx
Also you can move the page with its class to another folder.
So you can get page URL from its class by putting a constant variable inside the class contain its URL, construct the page URL your self, or wait till the page loads and by then you can get it by another way: Request.Url
It's possible to have multiple ASPXs inherit from a single class, though this is rarely used.
A side effect of this is that there's no tie between the class and the markup other than the inherits attribute in the Page tag in the markup so, no, you can't find the url from the class. You could work something out by adding a custom attribute to your page class, like:
[MyUrlAttribute("/somefolder/MyPage.aspx")]
class MyPage : Page
{
}
and then accessing that via reflection, or just set a static property on the page itself. r you could scan the aspx's at application start, parse the @page tag to grab the inherits="..." value, and save those in a table for later use. There are lots of potential solutions to this problem but no property like "Page.AspxPath."
Note that, as others have suggested, you can always use the Request.Url if you're just processing a normal request. If, however, you're trying to render a page when you either a) don't have a valid Request context, or b) want to render an aspx other than what was requested based on a class, you'll have to do some magic.
精彩评论