是否可以以在我的网址末尾检测到“.json”或“.xml”的方式设置路由?我将如何阅读,是否可以通过向我的操作方法添加参数来不读取该参数?我宁愿不为此目的使用查询字符串,这对我来说似乎很难看。
MyWebsite/Controller/MyAction.json
MyWebsite/Controller/MyAction.xml
MyWebsite/Controller/MyAction.otherType
---
public ActionResult MyAction()
{
var myData = myClient.GetData();
return SerializedData(myData);
}
private ActionResult SerializedData(Object result)
{
String resultType = SomeHowGetResultTypeHere;
if (resultType == "json")
{
return Json(result, JsonRequestBehavior.AllowGet);
}
else if (resultType == "xml")
{
return new XmlSerializer(result.GetType())
.Serialize(HttpContext.Response.Output, sports);
}
else
{
return new HttpNotFoundResult();
}
}