2

i was reading a article on mvc for uncontrolled action http://www.codeproject.com/Articles/874284/ASP-NET-MVC-Performance-Tips?msg=5003414#xx5003414xx

they write this code for uncontrolled action

protected override void HandleUnknownAction(string actionName)
{
       RedirectToAction("Index").ExecuteResult(this.ControllerContext);
}

author is saying that if a user request http://localhost:58234/default/index1, index1 action which does not exist so it will redirect to the default/index

i want to develop a base controller and put a routine which detect action method or controller exist or not for request url. if not exist then it will redirect user to a action method of a specific controller with query string value. as a result we can show a friendly message to user.

still i got no chance to work with mvc in office. just out of curiosity i like to know how to achieve the functionality with base controller?

help me with sample code. thanks

1

2 Answers 2

9

You can simply create base class as follows

 public class BaseController : Controller
    {
        protected override void HandleUnknownAction(string actionName)
        {
            //Your code
            RedirectToAction("Index").ExecuteResult(this.ControllerContext);
        }
    }

And your all controller should inherit BaseController instead of Controller.

UseCase

YOURDOMAIN.COM\CorrectController\InCorrectActionMethod // `HandleUnknownAction` will be called

YOURDOMAIN.COM\InCorrectController\InCorrectActionMethod // `HandleUnknownAction` will be not be called, in this case 404 will be thrown.
Sign up to request clarification or add additional context in comments.

7 Comments

How does HandleUnknownAction get invoked?
So whenever if invalid action method is found then this HandleUnknownAction , will be invoked, in-case if controller name it self is not correct then this it will not be invoked.
Who tells MVC handler to invoke this method and not HandleUnknownAction123()?
Method name it self describe its duty, Check this link msdn.microsoft.com/en-us/library/… this method will Called when a request matches this controller, but no method with the specified action name is found in the controller.
You are right, I didn't know about this method in Controller class. +1
|
2

Why you need to Do like So. Just do like this . Manage Application_Error event form Global.asax file .

protected void Application_Error(Object sender, EventArgs e)
    {

        //process 404 HTTP errors
        var httpException = exception as HttpException;
        if (httpException != null && httpException.GetHttpCode() == 404)
        {
                Response.Clear();
                Server.ClearError();
                Response.TrySkipIisCustomErrors = true;

                // Call target Controller and pass the routeData.
                IController errorController = "Your Controller";

                var routeData = new RouteData();
                routeData.Values.Add("controller", "Common");
                routeData.Values.Add("action", "PageNotFound");

                errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));

        }
    }

Hope this help .

8 Comments

this line is giving error EngineContext.Current.Resolve<IWebHelper> not compiling. do i need to include any assembly?
does your solution handle when user give incorrect controller or action name ?
IWebHelper used as application global interface to get common thing as error controller .
can u plzz give full code as a result i could understand the usage of IWebHelper.
I am working on multi tired application .EngineContext.Corrent return a Singletone instance .Resolve using for D I .IWebHelper class contain property like ErrorController etc ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.