简体   繁体   中英

Url Rewrite Angular ASP.NET

I am using URL Rewriting so that if the user reloads the page it he should land at the same view which he reloaded. To some extent i got what i wanted. I have 3 directories Admin and Users and a Root. I have written rewrite rules for all three cases. Individually all three are working fine ie if i use one at a time it works fine for the respective directory but if try to reload a page in other directory the url remains the same but the page rendered is from the other directory. For Instance i have a page opened under users directory and the view loaded is myprofile so now if i have the the rule for user directory kept first it will work fine but in this case suppose im under admin then if i reload the url will be same but the page rendered will be the default page for user but the view loaded will be from admin. And same thing happens for other case. Below are my rules

   <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>

I m not able to make out where mi going wrong. Because individually both rules are working fine. but when both are included. the base page rendered is changing though the view loaded is correct. Below is my Directory structure

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

Any help will be appreciated. Thanks!

After struggling for lot of days to figure this out still couldn't solve this but came up with a alternate solution. Sharing the solution. Hope it may help others out there. Our aim here is to rewrite the url so that we land actually at the view which we have requested. So for that we i used this approach we have a Application_BeginRequest event/method for a asp.net application. Which is executed just before every request is processed by the server. We can use this method for url rewriting Here is a simple example.

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");
    }

}

So the above code will rewrite the respective url based on the requested url. same as url rewrite. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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