英文:
Blazor WebAssembly client side routing: Tenant name in browser location
问题
我有一个Blazor WebAssembly应用程序,想要从URI中识别租户,并始终在浏览器地址栏中显示租户别名。
在服务器端,我已经添加了一个中间件,它从请求URI路径中识别租户并管理路由 - 到目前为止效果很好。
但在客户端,我不太明白:当我导航到https://domain/tenant
时,一切都很好,应用程序启动并显示页面。但当我点击链接导航到任何其他页面时,浏览器地址栏中的URI看起来像https://domain/page_route
,而不是https://domain/tenant/page_path
。
所以我的问题是:如何拦截路由,以便租户别名(来自Tenant.Current.Alias
)始终显示在浏览器地址栏中?
示例:
- 用户调用
https://domain
,使用默认租户tenant
- 用户点击链接,路由到
page_path
,这应该导致URI为https://domain/tenant/page_path
另一个示例:
- 用户调用
https://domain/tenant
- 用户点击链接,路由到
page_path
,这应该导致URI为https://domain/tenant/page_path
顺便说一句,对于API请求,我已经修改了客户端的HttpClient
注入,并将BaseAddress
更改为包含租户别名的URI。这对API请求有效,但当然不影响浏览器地址栏中显示的URI。
更新:目前我得出结论,我需要编辑所有页面路由,而不是在服务器端进行URI重写。
英文:
I have a Blazor WebAssembly app and would like to identify the tenant from the URI - and also display the tenant alias in the URI of the browsers address bar at all time.
At the server side I've added a middleware which identifies the tenant from the request URI path and manages the routing - which works great so far.
But at the client side I just don't get it: When I navigate to https://domain/tenant
, everything is fine, the app starts and displays a page. But when I click on a link and navigate to any other page, the URI in the browsers address bar looks like https://domain/page_route
- instead of https://domain/tenant/page_path
.
So my question is: How can I intercept the routing in a way that the tenant alias (which comes from let's say Tenant.Current.Alias
) is visible in the browsers address bar always?
Example:
- The user calls
https://domain
, which uses the default tenanttenant
- The user clicks on a link which routes to
page_path
, which should result in the URIhttps://domain/tenant/page_path
then
Another example:
- The user calls
https://domain/tenant
- The user clicks on a link which routes to
page_path
, which should result in the URIhttps://domain/tenant/page_path
then
By the way, for API requests I've modified the HttpClient
injection at the client and changed the BaseAddress
to the URI which includes the tenant alias. This works for API requests, but of course it doesn't affect the URI which is being displayed in the browsers address bar.
UPDATE: For now I came to the conclusion, that I need to edit all page routes, and there's no way like URI rewriting at the server side.
答案1
得分: 1
所以,最终我成功了:关键是修改引导性的 index.html
中的 base
标签,使其包含租户别名。
为此,我删除了 index.html
,创建了一个名为 Index.cshtml
的新服务器页面,并将默认路由更改为使用 Index.cshtml
,然后查找 index.html
。接着需要重定向浏览器,调用请求URI路径中不包含租户别名的URI。
在客户端,我更新了导航以使用 @("/"+tenantAlias+"/anything")
作为超链接。在Razor页面中,我可以从路由中删除租户别名,因为导航管理器使用 base
超链接并将其与页面路由结合在一起。
这在服务器端要复杂得多,但似乎是正确的方法,因为无法修改 NavigationManager
使用的URI,并且该基本URI仅在引导过程中初始化一次(这就是为什么需要浏览器重定向)。
英文:
So finally I did it: The trick is to modify the base
tag in the bootstrapping index.html
so it includes the tenant alias.
For this I deleted the index.html
, created a Index.cshtml
as new server page and changed the default routing to use the Index.cshtml
, before looking for a index.html
. Then it was required to redirect a browser, which calls an URI without the tenant alias in the request URI path.
At the client I updated the navigation to use @("/"+tenantAlias+"/anything")
as href. In the Razor pages I could remove the tenant alias from the routes, since the navigation manager uses the base
href and combines it with a page route.
This is way more complicated as at the server side, but it seems to be the correct way, since it's not possible to modify the URI which is used by the NavigationManager
, and this base URI is initialized only once during bootstrapping (that's why a browser redirect is required).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论