0

On my website, I had an old angular directive thats not in use anymore but a lot of search engine providers are trying to hit a particular url on my site:

/Spn/{{adId}}

Here is the ActionMethod:

public ActionResult Spn(int adId)
{
    .. my code here

    return RedirectToAction("Index");
}

Everytime this happens an errors gets logged to elmah, but this is becoming really annoying to see these errors in Elmah, what is the best way to prevent this error from logging? The error occurs before the code is hit inside the action method.

Here is an error: [ArgumentException: The parameters dictionary contains a null entry for parameter 'adId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Spn(Int32)

TheWebGuy
  • 11,429
  • 16
  • 52
  • 72

2 Answers2

0

try to make the action parameter nullable..

    public ActionResult Spn(int? adId)
    {
        .. my code here

        return RedirectToAction("Index");
    }
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
  • I am not really a fan of doing that, I'd prefer to handle the error before it reaches the action method, this means I will have to put a null check within my action method. – TheWebGuy Oct 01 '15 at 13:13
0

If your action method is "not in use anymore", why does it exist? You should remove it. That will make your server return a 404 response so the search engines will be able to remove it from their index.

Alternatively, you could block access to the URL by setting up an IgnoreRoute.

RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="[Ss]pn"});

If you want to direct the traffic that is still going to that URL somewhere else, then setup a 301 response using the IIS rewrite module so that traffic doesn't attempt to go to the action method and so search engines will know to remove the URL from their index and replace it with the new URL.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="spn2index" stopProcessing="true">
                <match url="^Spn/?.*$" />
                <action type="Redirect" url="Index" redirectType="Permanent" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

References:

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212