Vikas Jagdale
Vikas Jagdale

Reputation: 1

In AngularJS how to return 404 status code for any invalid URL?

Site is developed in AngualrJS and deployed on IIS web server. Where as we have only on page i.e. Index.html since it is single page application. For SEO purpose we are using prerender.io.

Added following code in web.config file to rewrite every single URL to root path

<rule name="AngularJS" 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>

Also handled all invalid URL into main.module.js with following code which will redirecting to error page.

$urlRouterProvider.otherwise(function ($injector, $location) {
            var $state = $injector.get('$state');
            var $rootScope = $injector.get('$rootScope');
            var now = new Date(); now.setTime(now.getTime() + 1 * 1800 * 1000);
            setContentCookie('dlerrorURL', $location.$$absUrl, now.toUTCString(), 'xyz.com')
            $rootScope.error = true;
            $state.go('app.newerror')
        });

With the help of above code, status code 404 getting appeared on prerender.io, but if we look into netowrk tab in developer console it displays 200 status code for all invalid URL. Technically it is correct since we have added rule in web.config file to rewrite all the URL to root path.

After lots of finding we came to the solution that we have to keep all the URLs at server side by implementing Asp.Net MVC code and adding all routes into route.config file. So that if the URL is not matched it will return 404 status code.

But since we are having 300+ pages we are avoiding to implement the above solution. Is there any alternat way to get or set 404 status code for invalid URL.

Upvotes: 0

Views: 872

Answers (1)

Mohammed Gadi
Mohammed Gadi

Reputation: 391

As per my experience so far, I faced the same issue and we made few changes like this and it worked like charm for us!

Follow this steps:

Step 1: Go to your RouteConfig.cs in your MVC application you will see some route map like this..

 routes.MapRoute(
              name: "Default",
               url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

Now comment that default route and add an additional map route like this

routes.MapRoute(
                name: "Home",
                url: "dv/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
             name: "CatchAll",
             url: "{*any}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

in above Map route name as HOME you will see something like dv you can replace it with any keyword for you like api or any initial of your project like abc or xyz we are doing this just to seperate the server route with angular route.

REMOVE THE CODE YOU HAVE WRITTEN IN YOUR WEB CONFIG FOR URL REWRITING. FOR PRERENDER:

Add something like this in your web.config

<configSections> 
<section name="prerender" type="Prerender.io.PrerenderConfigSection, Prerender.io, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<prerender  token="YOUR_TOKEN_HERE"></prerender>

and add prerender.io DLL to your MVC project.

Now, in system.web add something like this for prerender.

<system.web> 
<httpModules>
<add name="Prerender" type="Prerender.io.PrerenderModule, Prerender.io, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null"/>
</httpModules> 
</system.web>

Now in the App_Start folder of your MVC application add this class name PreApplicationStartCode.cs.

public static class PreApplicationStartCode
    {
         private static bool _isStarting;
        public static void PreStart()
        {


                if (!_isStarting)
                {
                    _isStarting = true;

                    DynamicModuleUtility.RegisterModule(typeof(Prerender.io.PrerenderModule));
                }

        }
    }

And in your AssemblyInfo.cs under Properties of your MVC app add something like this for prerender at the last line of the file.

[assembly: PreApplicationStartMethod(typeof(yourappnamespace.PreApplicationStartCode), "PreStart")]

Upvotes: 1

Related Questions