简体   繁体   English

是否可以动态加载 an.aspx 页面?

[英]Is it possible to dynamically load an .aspx page?

I received a new requirement today: For our product page, they want a completely different layout to be used based on the product type.我今天接到一个新需求:对于我们的产品页面,他们希望根据产品类型使用完全不同的布局。

For example, say we sell buckets.例如,假设我们卖水桶。 Currently, all buckets use the exact same page layout.目前,所有存储桶都使用完全相同的页面布局。 But now, they want wooden buckets to use the current layout, and plastic buckets to use a completely different layout.但是现在,他们希望木桶使用当前的布局,而塑料桶使用完全不同的布局。 However, they want the URLs to stay the same (eg, domain.com/bucket/1), so I can't just forward plastic buckets to a new page.但是,他们希望 URL 保持不变(例如,domain.com/bucket/1),所以我不能只将塑料桶转发到新页面。

The current page structure is as follows:当前页面结构如下:

CurrentMasterPage.master > CurrentProductPage.aspx > Several UserControls

The new layout requires new pages (ie, none of the current ones are reused):新布局需要新页面(即,当前页面均未重复使用):

NewMasterPage.master > NewProductPage.aspx > Several UserControls

My first thought was to take all of the markup and code from CurrentProductPage.aspx and put it into a UserControl, then create a second UserControl for the new layout (NewProductPage.aspx), and have CurrentProductPage.aspx dynamically load the appropriate UserControl based on the product type, however, this doesn't work because the new layout requires a new MasterPage, and I can't reference a MasterPage from a UserControl.我的第一个想法是从 CurrentProductPage.aspx 获取所有标记和代码并将其放入 UserControl,然后为新布局创建第二个 UserControl (NewProductPage.aspx),并让 CurrentProductPage.aspx 根据产品类型,但是,这不起作用,因为新布局需要新的 MasterPage,而我无法从 UserControl 引用 MasterPage。

Then, I thought about using URL Rewriting, but I don't think it's possible to have the same URL load two different pages.然后,我想到了使用 URL 重写,但我认为不可能让同一个 URL 加载两个不同的页面。

Is there any way to accomplish this?有什么办法可以做到这一点?

Why not use a 100% server side re-direct?为什么不使用 100% 服务器端重定向?

When you use response.Redirect("some different page").当您使用 response.Redirect("some different page") 时。 Then the client side browser is sent a whole new copy of that page, and the URL will be updated.然后客户端浏览器发送该页面的全新副本,URL 将被更新。

However, the server side can write any page it wants to.但是,服务器端可以写任何它想写的页面。 The client side will not even know the server decided to dish out a different page for the given URL.客户端甚至不知道服务器决定为给定的 URL 抛出一个不同的页面。

So, you could have a page with fake tabs as buttons.因此,您可以拥有一个带有假选项卡作为按钮的页面。 When the user hits a button, the browser round trip starts (for the given URL).当用户点击一个按钮时,浏览器往返开始(对于给定的 URL)。 But on server side, you can then dish out a different page for that URL.但是在服务器端,您可以为该 URL 提供不同的页面。

So, in place of this classic "round trip", you can use:因此,代替这个经典的“往返”,您可以使用:

Server.TransferRequest("MyotherWebPage")

So, for the given URL, before the current page (based on given URL) is sent down back to the browser, the above will simply pump out a different page.因此,对于给定的 URL,在当前页面(基于给定的 URL)被发送回浏览器之前,上面的代码将简单地抽出一个不同的页面。 The current page will never make it back down to the browser.当前页面永远不会返回到浏览器。

In fact for a rich page with lots of buttons and features, you can change the page displayed.事实上,对于包含大量按钮和功能的丰富页面,您可以更改显示的页面。 So in on-load - simply in place of a "response.Redirect", use a server.Transfer.所以在加载时 - 简单地代替“response.Redirect”,使用 server.Transfer。 The current page never makes it to the client - the one you dish out where.当前页面永远不会到达客户端 - 你抛出的那个页面。 Because the client side has zero clue about what the web server decides to dish out - it will also have zero clue that a different page was send back to the client.因为客户端对 web 服务器决定发送什么内容的线索为零 - 它也对将不同的页面发送回客户端一无所知。

Try the above with a test page.用测试页试试上面的方法。

On page A, behind a standard button, jump to web page B在A页,一个标准按钮后面,跳转到web B页

eg:例如:

Response.Recdirect("MyPageB.aspx")

Note the URL change - classic round trip.注意 URL 变化——经典往返。

Now, do this with the button:现在,使用按钮执行此操作:

Server.Redirect("MyPageB.aspx")

In this case, no full round trip occurs.在这种情况下,不会发生完整的往返行程。 The server transfers directly to the new page and sends that out.服务器直接转移到新页面并将其发送出去。 (and note how your URL does NOT change). (并注意您的 URL 如何保持不变)。

You can change the Master Page on PreInit on the Page using a Master.您可以使用母版在页面上的PreInit上更改母版页。 This is possible because a Master is basically the same as a User Control and is loaded AFTER the page's code behind.这是可能的,因为主控件与用户控件基本相同,并且在页面代码隐藏之后加载。

protected void Page_PreInit(object sender, EventArgs e)
{
    if (NewProductPage)
    {
        MasterPageFile = "~/NewMasterPage.master";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM