Blazor WebAssembly托管部署的URL重写例外 [英] URL Rewrite exceptions for Blazor WebAssembly Hosted deployment

查看:155
本文介绍了Blazor WebAssembly托管部署的URL重写例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发过程中,我在Blazor WebAssembly应用程序的服务器端使用了Swagger.始终使用kestrel而不是IIS Express启动(调试).路由按预期工作,我的所有组件都正确路由,如果我手动键入/swagger,我进入了swagger页面.一切都很好.

During development, i have used Swagger on the server side of my Blazor WebAssembly App. Always launching (debug) using kestrel instead of IIS Express. Routing worked as expected, all my component routed properly and if i manually typed /swagger, i got to the swagger page. All good.

我们已经在预生产服务器上的IIS下部署了服务器端和Blazor WebAssembly应用程序(客户端),它们可以按预期工作并且可以使用,但是,/swagger URL被重写(我假设)以放置在我的应用程序中与其让它进入Swagger,显然没有任何组件可以响应/swagger.

We have deployed under IIS on our pre-prod servers, the Server side and Blazor WebAssembly App (client) work as expected and are usable, however, my /swagger url gets rewritten (I assume) to go somewhere in my App instead of letting it go to Swagger, obviously there isn't any component that answers to /swagger.

我唯一的猜测是,当托管在IIS上时,aspnet核心应用程序会告诉IIS重写什么以及如何重写(类似于可以通过web.config为独立"部署提供的配置.)

My only guess is that, when hosted on IIS, the aspnet core app takes care of telling IIS what to rewrite and how (similar to the configs that could be provided thru a web.config for a "Standalone" deployment.)

我找不到如何指定例外情况,我一直在关注文档 https://docs.microsoft.com/zh-CN/aspnet/core/host-and-deploy/blazor/webassembly?view=aspnetcore-3.1#iis

I can't find how to specify exceptions, I've been following the doc at https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/blazor/webassembly?view=aspnetcore-3.1#iis

有什么主意我可以为/swagger添加一个例外吗?

Any idea how i could add an exception for /swagger ?

结果证明它可以在Chrome中正常运行,只有Firefox出现了不良行为.如果我清除缓存或使用隐身模式,则在Firefox中不会发生此问题.因此,似乎Firefox缓存了一些东西,并尝试将我的URL输入发送到Blazor Wasm,而不是直接发送到服务器.我将使用开发工具调试更多内容,并打开小提琴手尝试找出答案,并进行报告.

Turns out it works without issues in Chrome, only Firefox has the unwanted behavior. If i clear my cache, or use Incognito mode, the issue does not happen in Firefox. So, it seems that Firefox caches some stuff and tries to send my URL input to the Blazor Wasm instead of going thru to the server. I will debug some more with the dev tools and fiddler open to try and figure it out, will report back.

推荐答案

结果证明,这是已发布的service-worker.js文件的一部分.开发与发布的内容有所不同(这很有意义).

Turns out there this is part of the service-worker.js file that is published. It is different in dev than what gets published (which makes sense).

在调试期间,无论是否处于隐身/私人模式,我都能在所有浏览器(Edge,Chrome和Firefox)上重现该问题.

During my debugging i was able to reproduce the issue on all browsers (Edge, Chrome and Firefox), regardless of being in Incognito/Private mode or not.

服务工作人员一旦运行,它将处理来自Blazor WebAssembly应用程序的cache/index.html的服务请求.

Once the service-worker is running, it handles serving requests from cache/index.html of the Blazor WebAssembly app.

如果进入Blazor WebAssembly客户端"wwwroot"文件夹,则将找到service-worker.js和service-worker.published.js.在service-worker.published.js中,您将找到一个看起来像这样的函数:

If you go into your Blazor WebAssembly Client "wwwroot" folder, you'll find a service-worker.js and a service-worker.published.js. In the service-worker.published.js, you will find a function that looks like this :

async function onFetch(event) {
    let cachedResponse = null;
    if (event.request.method === 'GET') {
        // For all navigation requests, try to serve index.html from cache
        // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
        const shouldServeIndexHtml = event.request.mode === 'navigate'
            && !event.request.url.includes('/connect/')
            && !event.request.url.includes('/Identity/');

        const request = shouldServeIndexHtml ? 'index.html' : event.request;
        const cache = await caches.open(cacheName);
        cachedResponse = await cache.match(request);
    }

    return cachedResponse || fetch(event.request);
}

只需遵循代码注释中的说明即可解决此问题.因此,我们最终为"/swagger"添加了一个排除项,如下所示:

Simply following the instructions found in the code comments is gonna fix the issue. So we ended up adding an exclusion for "/swagger" like so :

&& !event.request.url.includes('/swagger')

希望这篇文章对那些想在服务工作者之外服务的人有用,而不仅仅是Swagger.

Hopefully this post is useful for people who are gonna want to serve things outside of the service worker, not only Swagger.

这篇关于Blazor WebAssembly托管部署的URL重写例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆