为 not found URl in web api 修改并重定向

Modify and redirect for not found URl in web api

我在 angular 中为我的项目启用了 Html5 模式 从

转换我的 URl

答:qwe.com/#/产品

B: qwe.com/products

但问题是在这种情况下,如果用户尝试直接转到 B,服务器(web api)捕获 Url 和 return not found 错误,所以我需要一个捕获所有未在服务器中找到的方法向其添加 # 符号并重定向到新的 Url 但我应该怎么做?

更新:

感谢@Travis Collins

在Global.asax

private const string ROOT_DOCUMENT = "/index.html";

protected void Application_BeginRequest( Object sender, EventArgs e )
{
   string url = Request.Url.LocalPath;
   if ( !System.IO.File.Exists( Context.Server.MapPath( url ) ) )
       Context.RewritePath( ROOT_DOCUMENT );
}

您需要为此在您的服务器端进行重写。这将使 Web 服务器仍然为您的 index.html 文件提供服务,即使请求是针对 /products 或其他任何内容。例如,在 IIS 中,您可以在 web.config:

中执行此操作
<system.webServer>
  <rewrite>
    <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

这里解释了许多其他服务器: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode