gpt4 book ai didi

javascript - 使用 page.js 进行 ASP.NET 客户端路由

转载 作者:行者123 更新时间:2023-11-27 23:08:39 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的 asp.net 应用程序(webforms),并且我想在客户端中进行路由。这是一个水疗中心。

我正在使用 page.js 库来处理路由。我的开发环境是Visual Studio 2015 Enterprise。这是一个路由示例

page('/admin', display_admin);
page('/items/:sub/:id', display_item);

在服务器端,我进行了设置,以便对于每个 url 请求,都会加载 index.html(因为路由是在客户端中处理的),并且调用并执行适当的路由处理程序。在 web.config 中我有:

<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<!--Redirect selected traffic to index -->
<rule name="Index Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

查看这篇文章ASP.NET 5 and AngularJS Part 3, Adding Client Routing

当自定义网址采用“/xxx”形式时,一切正常,例如“/admin”、“/items”等...但对于路径为“/xxx”形式的复杂网址则不起作用/yyy/zzz'。例如:“元素/汽车/10”。在 Visual Studio 中调试期间应用程序崩溃:

enter image description here

您可以看到服务器(Visual Studio 中的嵌入式 Web 服务器)尝试从“localhost:port/items/c/”文件夹中加载页面“1”的资源,该文件夹当然不存在。此请求不会到达我的服务器,其中会发生重定向到页面“index.html”。

如何在 asp.net(使用 .html 文件,而不是 .aspx 文件)和复杂的 url 路径中实现客户端路由?

我猜测这是与 Visual Studio 中的嵌入式 Web 服务器相关的问题(这是一个轻量级的 IIS 版本...)

任何帮助,任何提示将不胜感激

感谢 stackoverflow 社区

最佳答案

您很可能使用脚本的相对路径。这意味着浏览器将假设正在提供的页面位于 /something/else/entirely 下,并会在那里请求它。使用绝对路径作为/bootstrap.min.js以确保其请求正确。

服务器可能会根据您的配置在内部重定向到 /index.html,但浏览器不知道这一点。

单级 /something 起作用的原因是浏览器仍然假设所有内容都位于根级别。多加一个斜杠,上下文就会向下移动一个“文件夹”。

关于javascript - 使用 page.js 进行 ASP.NET 客户端路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36374785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com