Url重写AngularASP.NET

Url Rewrite Angular ASP.NET

我正在使用 URL 重写,这样如果用户重新加载页面,他应该会进入他重新加载的同一视图。在某种程度上,我得到了我想要的。 我有 3 个目录 Admin 和 Users 以及一个 Root。我已经为所有三种情况编写了重写规则。单独地,这三个都工作正常,即如果我一次使用一个,它对各自的目录工作正常,但如果尝试在其他目录中重新加载页面,url 保持不变,但呈现的页面来自其他目录。 例如,我在用户目录下打开了一个页面,加载的视图是我的配置文件,所以现在如果我首先保留用户目录的规则,它将正常工作,但在这种情况下,假设我在管理员之下,然后如果我重新加载 url 将相同,但呈现的页面将是用户的默认页面,但加载的视图将来自管理员。其他情况也会发生同样的事情。 以下是我的规则

   <rule name="Admin Redirect Rule" stopProcessing="true">
      <match url="/(Admin)*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/Admin/Admin.aspx" />
    </rule>

    <rule name="User Redirect Rule" stopProcessing="true">
      <match url="/(Users)*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/Users/User.aspx" />
    </rule>

我不知道哪里出了问题。因为这两个规则单独运行良好。但是当两者都包括在内时。尽管加载的视图是正确的,但渲染的基页正在改变。 下面是我的目录结构

Root
----Users
     -----User.aspx
----Admin
     -----Admin.aspx

如有任何帮助,我们将不胜感激。 谢谢!

在苦苦思索了很多天之后仍然无法解决这个问题,但想出了一个替代解决方案。分享解决方案。希望它可以帮助其他人。我们的目标是重写 url 以便我们实际到达我们请求的视图。因此,为此我们使用了这种方法,我们有一个 Application_BeginRequest event/method 用于 asp.net 应用程序。这是在服务器处理每个请求之前执行的。我们可以用这个方法url重写这里有一个简单的例子

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // Get current path
    string CurrentPath = Request.Path;
    HttpContext MyContext = HttpContext.Current;

    Regex regex = new Regex(@"^\/");
    Match match = regex.Match(CurrentPath);
    // Now we'll try to  rewrite URL in form of example format:
    // Check if URL  needs rewriting, other URLs will be ignored
    if (CurrentPath.IndexOf("/Users/") > -1 )
    {
        // I will use  simple string manipulation, but depending
        // on your case,  you can use regular expressions instead
        // Remove /  character from start and beginning
        // Rewrite URL to  use query strings
        MyContext.RewritePath("/Users/User.aspx");
    }
    else if (CurrentPath.IndexOf("/Admin/") > -1)
    {
        MyContext.RewritePath("/Admin/Admin.aspx");
    }

    else if (match.Success)
    {
        MyContext.RewritePath("/Default.aspx");
    }

}

所以上面的代码会根据请求的url重写相应的url。与 url 重写相同。希望对你有帮助。