1
是否可以通过它在我的url结尾检测到“.json”或“.xml”来设置路由? 而我怎么读,是否有可能不通过添加一个参数到我的操作方法读取该参数? 我宁愿不使用查询字符串来达到这个目的,它对我来说似乎很难看。如何检测ASP.NET MVC 4中的自定义URI?
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();
}
}