是否可以在我的 Web 应用程序中覆盖相对路径 ~/ ?

发布于 2024-09-28 01:13:40 字数 1136 浏览 3 评论 0原文

背景故事
我目前正在更新现有的 Web 应用程序以支持多租户环境。今天,我们当前在 http://www.example.com/MyApp 上托管一个应用程序,但现在我们必须能够在同一个 Web 应用程序上支持多个客户端。

因此每个客户端应该能够通过如下 URL 访问应用程序: http://www.example.com/MyApp/Client1
http://www.example.com/MyApp/Client2

Web 应用程序是 ASP.NET我们也正在升级以使用 .NET 4 的 webforms 应用程序。我已经实现了 ASP.NET 4 中可用的新路由,并且已经放置了适当的路由来支持多租户架构。

示例 URL http://www.example.com/MyApp/Client1/SomeModule/ SomePage.aspx 将路由到并执行 /MyApp/SomeModule/SomePage.aspx 并且我可以从 RouteData 中访问客户端部分。

但是...

主要问题:
我们在应用程序的许多部分都有使用 url 的链接,例如 ~/SomeModule/SomePage.aspx,因此当它们呈现到浏览器时,它们仍然会显示 /MyApp/SomeModule/SomePage。 aspx,但我真正想要的是让它包含我的路由值之一来注入客户端页面,例如 MyApp/Client1/SomeModule/SomePage.aspsx

有没有办法覆盖什么~/ 意味着在我的整个应用程序中而不需要遍历我的整个应用程序并更新链接?

Back Story
I am currently updating an existing web application to support a multi-tenant environment. Today we current host an application on http://www.example.com/MyApp, but now we must be able to support multiple clients on the same web application.

So each client should be able to access the application through urls like:
http://www.example.com/MyApp/Client1
http://www.example.com/MyApp/Client2

The web application is an ASP.NET webforms app that we are also upgrading to use .NET 4. I have already implemented the new routing available in ASP.NET 4, and I haven already put the appropriate routes in place to support the multi-tenant architecture.

The example URL http://www.example.com/MyApp/Client1/SomeModule/SomePage.aspx will route to and execute /MyApp/SomeModule/SomePage.aspx and I have access to the client part from within the RouteData.

BUT...

Main Question:
We have links in many parts of the application that use url's like ~/SomeModule/SomePage.aspx, so when they render out to the browser they will still show /MyApp/SomeModule/SomePage.aspx, but what I really want is to have it contain one of my route values to inject the client page like MyApp/Client1/SomeModule/SomePage.aspsx

Is there a way to override what the ~/ means throughout my application without going through my entire app and updating the links?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-10-05 01:13:40

将 Page 对象上的 AppRelativeVirtualPath 属性设置为所需的路径。但是您必须在创建处理程序后首先执行此操作,因为添加到 Controls 集合中的每个控件都将从其父级继承此值。

实现此目的的一种方法是子类化 PageHandlerFactory 类,该类负责在请求 .aspx 文件时创建 Page 实例。使用以下代码覆盖 GetHandler

public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
    var handler = (Page)base.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
    page.AppRelativeVirtualPath = "...";

    return page;
}

当然,更改 web.config 中 httpHandlers 下的注册以指向新的工厂类。

Set the AppRelativeVirtualPath property on your Page object to your desired path. But you have to do it as the first thing after your handler is created, since every control added to your Controls-collection will inherit this value from its parent.

A way to do this is to subclass the PageHandlerFactory class which is responsible for creating a Page instance when your request a .aspx file. Override the GetHandler with the following code:

public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
    var handler = (Page)base.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
    page.AppRelativeVirtualPath = "...";

    return page;
}

and of course change the registration under httpHandlers in your web.config to point to your new factory class.

梦里泪两行 2024-10-05 01:13:40

我似乎不可能覆盖 ~/ 。我们决定采用不同的方法并创建实用方法来帮助管理多租户环境中的链接和路径。

我看到实际实现此目的的最佳选择是实现 IUrlResolutionService,但我们无法成功完成这项工作。

I doesn't seem like overriding the ~/ is possible. We have decided to take a different approach and create utility methods to help manage links and paths within our multi-tenant environment.

The best option I saw to actually accomplish this is to implement IUrlResolutionService, but we were unable to make this work successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文