0

我正在使用 URL 重写,以便如果用户重新加载页面,他应该在他重新加载的同一视图中着陆。在某种程度上,我得到了我想要的。我有 3 个目录 Admin 和 Users 以及一个 Root。我已经为所有三种情况编写了重写规则。单独所有三个都工作正常,即如果我一次使用一个,它适用于相应的目录,但如果尝试在其他目录中重新加载页面,则 url 保持不变,但呈现的页面来自另一个目录。例如,我在用户目录下打开了一个页面,加载的视图是 myprofile,所以现在如果我首先保留用户目录的规则,它将正常工作,但在这种情况下,假设我在管理员下,那么如果我重新加载 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

任何帮助将不胜感激。谢谢!

4

1 回答 1

0

在努力解决了很多天之后仍然无法解决这个问题,但想出了一个替代解决方案。分享解决方案。希望它可以帮助其他人。我们的目标是重写 url,以便我们实际到达我们请求的视图。因此,为此我们使用了这种方法,我们Application_BeginRequest为 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 重写相同。希望能帮助到你。

于 2016-05-09T08:59:22.550 回答