Friday 8 January 2010

MVC: Creating custom attributes using "ActionMethodSelectorAttribute".

Hi,

Welcome back!

In MVC application you have encounter many MVC attributes like HandleError and many more. These attributes servers one particular task so that you do not have to re-write the code for for it.

If you want to write your attribute in controller, then how can we do that?
MVC provides one class named ActionMethodSelectorAttribute, which can help us to create our own custom attribute. Let's create our own attrubite "AjaxMethodAttribute". This attribute tests the Ajax request. If the request from Ajax method came, allow the action to execute. The request other than the Ajax should be neglected.


We will create the class named AjaxMethodAttribute inherited from ActionMethodSelectorAttribute.
The class ActionMethodSelectorAttribute has one override method IsValidForRequest which has to be implemented.
Here is our implementation.

public class AjaxMethodAttribute : ActionMethodSelectorAttribute
    {
        ///
        /// Implement IsValidForRequest method as per your need.
        ///
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
// Check request of Ajax.
            return controllerContext.HttpContext.Request.IsAjaxRequest();
        }
    }

You can now have our custom attribute ready for use. Let's try it.

[AjaxMethodAttribute] public ActionResult AjaxRequest() { ViewData["AjaxCall"] = "Call from Aajx Request"; return View("AjaxRequest"); }

Hope you enjoy this blog. Keep watching for more blogs...

MVC: Handling controller's unknown action using "HandleUnknownAction" method.

Hi,

Welcome back.

Whenever the user for a action / method which does not exists in any controller, the framework shows the error message. How to handle it?

Each controller has one override method named "HandleUnknownAction". You can use it to handle the unknown request. The method accept one parameter actionname.

        /// Method to process unknown action request.
        /// Unknwoan action name

        protected override void HandleUnknownAction(string actionName)
        {
            ViewData["actionName"] = actionName;
            View("Error").ExecuteResult(this.ControllerContext);
        }

You can modify the Error.aspx to show the appropriate message as shown below:


        Sorry, I don't recognize the action <b> <%= Html.Encode(ViewData["actionName"]) %> </b>.


Keep watching my blog for more...

MVC: How to call a view stored in different folder other than the conventional.

Hi,

Welcome back!

In MVC your can return the View which will be view of the same Controller or any other existing controllers. If the view does not exist the framework search the view in Views/Shared folder.


Now, suppose you have to call the view stored in some folder MyViews under the Views folder. There is no controller with name MyViews. How to call this view then?

Here is the solution:

return View("../MyViews/TestView");

You can call this view using relative path as shown above.

Hope you have enjoy this blog.
Keep watching my blog...