1

In my ASP.NET SPA application I am using web.config to set Rewrite Rules, so I can refresh pages without error (while using Angularjs), but I also would like to redirect to application's homepage, when non-existing page manually entered into url, please advise how should I modify code below to accommodate this:

<rewrite>
    <rules>
      <rule name="Products" stopProcessing="true">
        <match url="^products" />
        <action type="Rewrite" url="/" />
      </rule>
      <rule name="Orders" stopProcessing="true">
        <match url="^orders" />
        <action type="Rewrite" url="/" />
      </rule>
      <rule name="Home" stopProcessing="true">
        <match url="^home" />
        <action type="Rewrite" url="/" />
      </rule>
      <rule name="List" stopProcessing="true">
        <match url="^list" />
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
superconsultant
  • 231
  • 2
  • 6
  • 20
  • Angular routing and asp.net "routing" should not be conflicting with each other, in that case, all your server routes should redirect to the root, from that, angular should take care of routing your SPA. I believe that you should add a 404 error page that redirects to your index, having not to do with url rewriting itself. – Fedaykin Aug 12 '14 at 19:38
  • 1
    Thanks for your reply, any example you can refer to, for such scenario. – superconsultant Aug 12 '14 at 19:48
  • Yes, I´m going to add it as an answer – Fedaykin Aug 12 '14 at 19:51

2 Answers2

1

As suggested by Fedaykin(thanks alot) the following code resolved this issue:

<system.webServer>
    <httpErrors errorMode="Custom">
       <remove statusCode="404"/>
       <error statusCode="404" path="/Default.aspx" responseMode="ExecuteURL"/>
    </httpErrors>
</system.webServer>

Since I work on Single Page Application, in my case path would equal to "/", like this:

<error statusCode="404" path="/" responseMode="ExecuteURL"/>

and then Angularjs routing will take care of the rest.

superconsultant
  • 231
  • 2
  • 6
  • 20
  • @Fedaykin i have followed your way in my we.config my problem not solved. can you assist me a little bit more – Zaid Iqbal Oct 11 '14 at 13:22
0

You can use a 404 error handling to redirect to your homepage (here I´m assuming it´s Default.aspx, but change it to whatever is your home), on your webconfig file put:

<configuration>

   <system.web>
      <compilation targetFramework="4.0" />

      <customErrors mode="On" redirectMode="ResponseRewrite">
         <error statusCode="404" redirect="Default.aspx" />
      </customErrors>
   </system.web>

   <system.webServer>
      <httpErrors errorMode="Custom">
         <remove statusCode="404"/>
         <error statusCode="404" path="/Default.aspx" responseMode="ExecuteURL"/>
      </httpErrors>
   </system.webServer>

</configuration>

Also, on your angular configuration use the following to redirect to home:

$routeProvider.
   when('/',{'templateUrl' : 'a.html'})
  .when('/b',{'templateUrl' : 'b.html'})
  .otherwise({redirectTo : '/'})
}

Hope that helps.

Fedaykin
  • 4,482
  • 3
  • 22
  • 32
  • 1
    It works like a charm. It even seems that section you have put between tags is good enough or do I still need the part for ?. Thanks a lot. I tried to mark your answer as helpful, but don't have enough reputation. – superconsultant Aug 14 '14 at 14:47
  • Just marked it as the answer. Thanks again. Hey if you know anything about Typeahead, I have another post I need help with: http://stackoverflow.com/questions/25312303/angularjs-ui-bootstrap-typeahead-implementation-issue, if you are in the mood feel free to take alook. – superconsultant Aug 14 '14 at 15:54
  • @Fedaykin I also have the same problem but i do not know why your solution did'nt help me can you you assist me little bit more. Here is my Question : http://stackoverflow.com/questions/26313790/how-to-set-default-page-in-angular/26314367#26314367 – Zaid Iqbal Oct 11 '14 at 13:26