使用page.js的asp.net客户端路由

时间:2016-04-02 14:56:01

标签: javascript asp.net html5 routing single-page-application

我正在尝试构建一个简单的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

当自定义网址为'/ xxx'格式时,一切正常,例如'/ admin','/ items'等...但它不适用于形式为'/ xxx的路径的复杂网址/ YYY / ZZZ”。例如:'items / cars / 10'。在Visual Studio调试期间,应用程序崩溃了:

enter image description here

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

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

我猜这是与visual studio中的嵌入式网络服务器有关的问题(这是一个简单的iis版本......)

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

谢谢stackoverflow社区

1 个答案:

答案 0 :(得分:1)

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

服务器可能会在内部使用您的配置重定向到/index.html,但浏览器并不知道。

单级/something工作的原因是浏览器仍假设一切都在根级别。另一个斜杠和上下文向下移动一个&#34;文件夹。&#34;