iT4MVCActionResult

Jul 23, 2012 at 12:14 PM

I am working with IT4MVCActionResult now and have a couple of questions:

1. Why T4MVC_ActionResult and other similar classes do not contain Area property (while there are properties for all other parameters passed to InitMVCT4Result)?

2. Why don't Controller and other similar properties do not set value to RouteValueDictionary (like it is done in InitMVCT4Result)? I guess it is a bug it is reproduced in rare cases when you need to set Area of ActionResult (like I do).

Here is the code I came to:

public class T4MVC_ActionResult : System.Web.Mvc.ActionResult, IT4MVCActionResult {
    public T4MVC_ActionResult(string area, string controller, string action, string protocol = null): base()  {
        this.InitMVCT4Result(area, controller, action, protocol);
    }
     
    public override void ExecuteResult(System.Web.Mvc.ControllerContext context) { }
    
	public string Area 
	{ 
		get
		{
			return RouteValueDictionary.ContainsKey("Area") ? (string)RouteValueDictionary["Area"] : String.Empty;
		}
		set
		{
		   RouteValueDictionary["Area"] = value;
		}
    }
    public string Controller
	{ 
		get
		{
			return (string)RouteValueDictionary["Controller"];
		}
		set
		{
			RouteValueDictionary["Controller"] = value;
		}
	}
    public string Action 
	{ 
		get
		{
			return (string)RouteValueDictionary["Action"];
		} 
		set
		{
			RouteValueDictionary["Action"] = value;
		}
	}
    public string Protocol { get; set; }
    public RouteValueDictionary RouteValueDictionary { get; set; }
}

public static void InitMVCT4Result(this IT4MVCActionResult result, string area, string controller, string action, string protocol = null) {
			result.RouteValueDictionary = new RouteValueDictionary();
		    result.Area = area;
            result.Controller = controller;
            result.Action = action;
            result.Protocol = T4MVCHelpers.IsProduction() ? protocol : null;
        }

What do you think?

Coordinator
Jul 31, 2012 at 12:44 AM

For #1, it essentially matches the way MVC works. When you call a method like ActionLink, it never takes the Area as a parameter, but it's passed as a route value. Though it probably won't work to have it.

For #2, I agree that would be a bit cleaner. Maybe the concern was that it would increase the amount of duplicated code that would be needed for each generated ActionResult class (there is one per distinct return type used in controller actions).