开发者

Is there a method that returns a System.Web.UI.Page class given an URL?

I'd like to be able to determine what class will be called given an URL.

For instance,

I have a page /First.aspx that has a hyperlink to /Second.aspx

In the code behind for First.aspx.cs, I'd like to be able to determine what class will execute if someone clicks on the hyperlink that points to /Second.aspx.

EDIT: One of the reponders asked me to outline the problem I'm trying to address. Here it is:

The codebase I inherited has a subclass the System.Web.UI.Page that has a public Authorized method that returns a boolean. The Authorized method checks the parameters passed via the query string against the authenticated user, and determines whether that user should be allowed to call that page with the given parameters.

Elsewhere in the site I have hyperlinks that reference those protected pages. In some instances those links are displayed to users who are not authorized to navigate to that page. They can click the link, however they get an error. In other instances, prior developers when through the trouble of inserting logic that hides th开发者_Go百科e hyperlink for those unauthorized users, but the authorization logic is duplicated (In the page itself, and in the linking page).

What I would like to do is create a subclass of the hyperlink class, and have the subclass inspect the NavigateUrl, determine the destination page class, and call the Authorized method of that class to determine if the user is authorized to call that page. If the user is not authorize, the link will automatically hide itself.


You can use BuildManager.CreateInstanceFromVirtualPath to build a page from a virtual url, then you can grab the type, like this:

var page = BuildManager.CreateInstanceFromVirtualPath("~/Second.aspx", typeof(Page));
var type = page.GetType();

Even though you're passing typeof(Page) in the method, the type will still be the type of your page, something like this: MyNamespace.Second


Well you could

  1. open Second.aspx as a file (using a StreamReader, for example),
  2. search for the <%@ Page ... %> directive and
  3. read the value of the Inherits="..." attribute.

However, it feels terribly wrong to do something like that. What is the real problem you are trying to solve? Maybe there's an easier way that does not involve such low-level file operations...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜