简体   繁体   English

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

[英]asp.net client routing with page.js

I am trying to build a simple asp.net application (webforms) and I would like to do the routing in the client.我正在尝试构建一个简单的 asp.net 应用程序(webforms),我想在客户端进行路由。 This is a SPA.这是一个SPA。

I am using the page.js library to handle the routing.我正在使用 page.js 库来处理路由。 My development environment is visual studio 2015 Enterprise.我的开发环境是visual studio 2015 Enterprise。 This is an example of a routing这是一个路由示例

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

In the server-side, I setup things so that for every url request, index.html is loaded (since routing is handled in the client) and the appropriate route handler is called and executed.在服务器端,我设置了一些东西,以便对于每个 url 请求,加载 index.html(因为路由是在客户端处理的)并调用和执行适当的路由处理程序。 In the web.config I have:在 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>

see this article ASP.NET 5 and AngularJS Part 3, Adding Client Routing请参阅本文ASP.NET 5 和 AngularJS 第 3 部分,添加客户端路由

Everything works fine when the custom url is of this form '/xxx' for example '/admin' , '/items' etc... But it does not work for complex url where the path of the form '/xxx/yyy/zzz'.当自定义 url 采用这种形式 '/xxx' 例如 '/admin' 、 '/items' 等时,一切正常...但它不适用于形式为 '/xxx/yyy/ 的路径的复杂 url zzz'。 ex: 'items/cars/10'.例如:“物品/汽车/10”。 In visual studio during debug the application craches:在调试期间在 Visual Studio 中应用程序崩溃:

在此处输入图片说明

You can see that the server (embedded webserver in visual studio) tries to load resources for page "1" from the folder at 'localhost:port/items/c/' which of course does not exist.您可以看到服务器(visual studio 中的嵌入式 Web 服务器)尝试从 'localhost:port/items/c/' 的文件夹中加载页面“1”的资源,这当然不存在。 This request does not reach my server where a redirect would occur to page 'index.html'.这个请求没有到达我的服务器,在那里重定向到页面“index.html”。

How can I implement client-side routing in asp.net (with .html files, not .aspx files) and with complex url path?如何在 asp.net(使用 .html 文件,而不是 .aspx 文件)和复杂的 url 路径中实现客户端路由?

I am guessing that this is problem related to the embedded webserver in visual studio (which is a light iis version...)我猜这是与visual studio中的嵌入式网络服务器有关的问题(这是一个轻量级的iis版本......)

Any help, any hint would be greatly appreciated任何帮助,任何提示将不胜感激

Thank you stackoverflow community感谢 stackoverflow 社区

You are most likely using relative paths to scripts.您最有可能使用脚本的相对路径。 This means the browser will assume the page being served is under /something/else/entirely and will ask for it there.这意味着浏览器将假定正在提供的页面位于/something/else/entirely并会在那里请求它。 Use an absolute path as /bootstrap.min.js to make sure it's requested properly.使用绝对路径作为/bootstrap.min.js以确保它被正确请求。

The server may internally redirect to /index.html with your configuration but the browser doesn't know that.服务器可能会使用您的配置在内部重定向到/index.html ,但浏览器并不知道这一点。

The reason why single level /something works is due to browser still assuming everything is on the root level.单级/something工作的原因是浏览器仍然假设一切都在根级别。 An additional slash and the context moves down one "folder."附加斜线和上下文向下移动一个“文件夹”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM