0

suppose this is my route which expect integer data for productid

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"\d+" }
 );

so these route will work

/Product/3
/Product/8999 

how could i redirect user to a preexisting view where i can show a friendly error message for this scenario?

one more things i like to know can we set status code when user will type /Product/apple and this route will not match any. so in this case i like to set statusCode="404". if i can do it then below customErrors where i specified a view will be shown for 404 error. suggest me how to do it?

<customErrors mode="Off" defaultRedirect="~/error.html">
  <error statusCode="403" redirect="~/access-denied.aspx" />
  <error statusCode="404" redirect="~/page-not-found.aspx" />
  <error statusCode="500" redirect="~/error.html" />
</customErrors>

please guide me if it is possible.

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • `/Product/apple` this request is current not matched by your route... Two things you can do... 1. remove productid regex parse and handle that in action 2. add new route that match string and call perticular action , in that action redirect to page you want – Vishal Sharma Dec 05 '17 at 12:43

1 Answers1

0

Your current listed route is not matching with the request you said.

You should consider adding two routes like this.

   routes.MapRoute(
                "Product",
                "Product/{productId}",
                new { controller = "Product", action = "Details" },
                new { productId = @"\d+" }
            );
            routes.MapRoute(
                "ProductNotMatched", 
                "Product/{productId}", 
                new { controller = "Product", action = "NotFound" });

This will now call NotFound action when /Product/apple will get hit.

Next you can create an action and redirect it like this,

public ActionResult NotFound()
        {
            return HttpNotFound();
**// HERE you can redirect to specific html / razor view or static page as you wish**
        }

Hope that helps

Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36
  • NotFound() action is not taking any parameter for productid. did u miss your action to decorate for the param productid or is it intentional ? – Thomas Dec 05 '17 at 13:05
  • this `return HttpNotFound();` set http status code 404 which redirect to my page page-not-found.aspx file ? – Thomas Dec 05 '17 at 13:06
  • Thats intentional , `/Product/apple` , I suppose you didnt need `apple` in Notfound , if you need that , you can add string param there. – Vishal Sharma Dec 05 '17 at 13:07
  • see my comment underneath that line , you need to add mvc view for that. ` ` this should work for mvc https://stackoverflow.com/a/13905859/1018054 – Vishal Sharma Dec 05 '17 at 13:09
  • i read this write up http://benfoster.io/blog/aspnet-mvc-custom-error-pages `ASP.NET issued a redirect to /404.html?aspxerrorpath=/foo/bar` specially see this line. we need to configure few more things in error page for 404 error. – Thomas Dec 05 '17 at 13:13