开发者

Can an ASPX page be given an alias so that it can be accessed from two different URLs?

开发者 https://www.devze.com 2023-03-19 05:40 出处:网络
I need to access the same page via two different names for the page. Example: CustomersDetail.aspx needs to be accessable using the aliased name PartnersDetail.aspx

I need to access the same page via two different names for the page.

Example:

CustomersDetail.aspx needs to be accessable using the aliased name PartnersDetail.aspx

CustomersDetail.aspx is the real file.

Both of the following urls should map to the same page:

http://www.example.com/CustomersDetail.aspx    
http://www.example.com/PartnersDetail.aspx

Is this possible using the Web.Config? If this is possible can the page kn开发者_运维知识库ow which url it was accessed from by looking that the request uri?


4GuysFromRolla does an excellent job of explaining ASP.NET 2.0's method of url mapping within the web.config which allows for very readable and easily maintainable url mapping.

Essentially you will want to put the following in your web.config inside of the system.web section:

<urlMappings enabled="true">
  <add url="~/PartnersDetail.aspx" mappedUrl="~/CustomersDetail.aspx" />
</urlMappings>


Depeding on the version of the .Net Framework (introduced in 3.5) you are using you could add an entry to the RouteTable.Routes collection in the Global.asax on application start:

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("DetailReRoute",
            "PartnersDetail.aspx", //Virtual Page
            "~/CustomersDetail.aspx", //Physical Page
            false, null, null);
    }


May be it is a bit tough, but if you won't find a clean solution and do not want to go deep into URL-rewriting why not create the second page and just put Response.Redirect (page needed - CustomersDetail.aspx - and its URL will be shown) or Server.Transfer (page needed and the URL http://www.example.com/PartnersDetail.aspx will be shown) into the Page_Load?


You could also put UserControls on each page.

I meant that you could create both pages, with the names you want and place the actual content inside an UserControl. After that, you put the UserControl in both pages. Whenever you want to change the content you just change in the UserControl and it will be replicated.

I know this is not the best solution, but works nice if you want aspx pages immediately.

0

精彩评论

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

关注公众号