1

i am new in asp.net mvc3 . i am building my site in which i am facing a problem that i have a controller named Home and a method in it named Index and this index method takes a parameter id now according to this situation a valid url should be like this "http://www. counterexample.com/Home/Index/1" but what should i do if a user types a url like this "http://counterexample.com/whatsisthis" . It will definately give 404 error but i want to do that when any one types a URL like this he should be redirected to my another controller named Thunder on method ErrorDetails and all the character which user types in url after / should come to this method as parameter like following

 public class ThunderController : Controller
{
    public ActionResult ErrorDetails (string id)
    {
        var params = id ; // and the text "whatisthis" should store in param . 
        return View();
    }

}

Please note that user can type any url like

  1. http://counterexample.com/whatsisthis/this
  2. http://counterexample.com/whatsisthis/this/call

i want that all these URL or any URL that doesn't exists should redirect to my method 'ErrorDetails' . Thanks

Ahsan
  • 271
  • 1
  • 8
  • 16

2 Answers2

1

You can setup custom error on the web.config file.

<customErrors mode="RemoteOnly" >
   <error statusCode="404" redirect="/Thunder/ErrorDetails/" />
 </customErrors> 

Once 404 error was thrown, the user will be redirected to the ErrorDetails view.

Pongsathon.keng
  • 1,417
  • 11
  • 14
0

Use custom errors (web.config) like this:

<customErrors mode="On" >
   <error statusCode="404" redirect="/Thunder/ErrorDetails/" />
   <!-- You can add other error codes or a generic one for other status codes here -->
</customErrors>  
robasta
  • 4,621
  • 5
  • 35
  • 53