自定义对象由MapRoutes定义的控制方法的参数 [英] Custom objects as arguments in controller methods defined by MapRoutes
本文介绍了自定义对象由MapRoutes定义的控制方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一下这个图路线:
图路线(
的resultFormat
{控制器} / {行动} / {ID} {}的resultFormat
新{控制器=家,行动=索引,ID = 0,的resultFormat =json的}
);
和它的控制器的方法:
公众的ActionResult指数(的Int32 ID,字符串的resultFormat)
{
变种DC =新Models.DataContext(); 从dc.Messages那里m.MessageId == ID选择M M VAR消息=; 如果(的resultFormat ==JSON)
{
返回JSON(短信,JsonRequestBehavior.AllowGet); // 2的情况下
}
其他
{
返回视图(消息); // 情况1
}
}
这里的URL方案
-
会去区分1
-
会去区分1
-
会去区分2
这工作得很好。但我讨厌检查字符串。将如何实施的枚举的被用作控制器方法的的resultFormat
参数?
有些伪code解释基本思想是:
命名空间模型
{
公共枚举的responseType
{
HTML = 0,
JSON = 1,
文字= 2
}
}
该图路线:
图路线(
的resultFormat
{控制器} / {行动} / {ID} {}的resultFormat
新{
控制器=家,
行动=索引,
ID = 0,
的resultFormat = Models.ResultFormat.HTML
}
);
控制器方法签名:
公众的ActionResult指数(的Int32 ID,Models.ResultFormat的resultFormat)
解决方案
这是我想出了ActionFilter:
公共密封类AlternateOutputAttribute:
ActionFilterAttribute,IActionFilter
{
无效IActionFilter.OnActionExecuted(ActionExecutedContext AEC)
{
的ViewResult VR = aec.Result为的ViewResult; 如果(VR == NULL)回报; VAR AOF = aec.RouteData.Values [alternateOutputFormat]作为字符串; 如果(AOF ==JSON)aec.Result =新JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
数据= vr.ViewData.Model,
的ContentType =应用/ JSON
ContentEncoding = Encoding.UTF8
};
}
}
Consider this MapRoute:
MapRoute(
"ResultFormat",
"{controller}/{action}/{id}.{resultFormat}",
new { controller = "Home", action = "Index", id = 0, resultFormat = "json" }
);
And it's controller method:
public ActionResult Index(Int32 id, String resultFormat)
{
var dc = new Models.DataContext();
var messages = from m in dc.Messages where m.MessageId == id select m;
if (resultFormat == "json")
{
return Json(messages, JsonRequestBehavior.AllowGet); // case 2
}
else
{
return View(messages); // case 1
}
}
Here's the URL scenarios
Home/Index/1
will go to case 1Home/Index/1.html
will go to case 1Home/Index/1.json
will go to case 2
This works well. But I hate checking for strings. How would implement an enum to be used as the resultFormat
parameter in the controller method?
Some pseudo-code to explain the basic idea:
namespace Models
{
public enum ResponseType
{
HTML = 0,
JSON = 1,
Text = 2
}
}
The MapRoute:
MapRoute(
"ResultFormat",
"{controller}/{action}/{id}.{resultFormat}",
new {
controller = "Home",
action = "Index",
id = 0,
resultFormat = Models.ResultFormat.HTML
}
);
The controller method signature:
public ActionResult Index(Int32 id, Models.ResultFormat resultFormat)
解决方案
This is the ActionFilter I came up with:
public sealed class AlternateOutputAttribute :
ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuted(ActionExecutedContext aec)
{
ViewResult vr = aec.Result as ViewResult;
if (vr == null) return;
var aof = aec.RouteData.Values["alternateOutputFormat"] as String;
if (aof == "json") aec.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = vr.ViewData.Model,
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
}
}
这篇关于自定义对象由MapRoutes定义的控制方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文