【发布时间】:2016-07-22 08:23:31
【问题描述】:
我正在尝试构建一个简单的 asp.net 应用程序(webforms),我想在客户端中进行路由。这是一个SPA。
我正在使用 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
当自定义 url 采用这种形式 '/xxx' 例如 '/admin' 、 '/items' 等时,一切正常...但它不适用于复杂的 url 形式的路径 '/xxx /yyy/zzz'。例如:“物品/汽车/10”。在 Visual Studio 中调试应用程序崩溃:
您可以看到服务器(Visual Studio 中的嵌入式网络服务器)尝试从 'localhost:port/items/c/' 的文件夹中为页面“1”加载资源,这当然不存在。此请求未到达我的服务器,在该服务器上会发生重定向到页面“index.html”。
如何在 asp.net(使用 .html 文件,而不是 .aspx 文件)和复杂的 url 路径中实现客户端路由?
我猜这是与 Visual Studio 中的嵌入式网络服务器相关的问题(这是一个轻量级的 iis 版本......)
任何帮助,任何提示将不胜感激
感谢 stackoverflow 社区
【问题讨论】:
-
你在脚本中使用绝对路径吗?
-
这是 page.js 的作品。它试图模仿 express.js 的行为,但在客户端
-
所以你没有在 index.html 中为 bootstrap 写参考
-
参考资料在那里。应用程序第一次使用 index.html 启动时,一切正常。显示 index.html。然后我输入一个自定义 url (匹配一个路由条目,例如 /item/:cat/:id (用于测试目的)然后它看起来像浏览器重新加载页面然后资源被搞砸了......但如果 url 只有在级别(例如'/item')我没问题
-
那么是 absolute 还是 relative 路径?我假设是相对的,所以浏览器只会尝试获取该路径上的所有内容。使用
/bootstrap.min.js
标签: javascript asp.net html routing single-page-application