2009-08-14 48 views
4

當請求http://someserver.com/user/btyndall 我想返回HTML 當請求http://someserver.com/user/btyndall?format=xml 我想回到我的模型ASP.NET MVC - 返回數據作爲HTML或XML

我已經下載MvcContrib的XML表示。 (我不能相信XmlResult不是核心框架的一部分)

在控制器中處理請求的正確方法是什麼?有了JSON,你有一個JsonResult和Json()。我看到一個XmlResult但不是一個Xml()方法

我可以使用一點指導。我至今(這是虛無縹緲):

public ActionResult Details(int id) 
{ 
    return View(); 
} 

UPDATE
查看所有評論關於剛剛返回兩種不同的觀點

+0

我將很快發佈到ASP.NET CodePlex站點http://aspnet.codeplex.com/,它將解決這種情況。敬請關注。 :) – Haacked 2009-08-14 16:24:22

+0

下週我需要一些原型。在此期間我應該採用什麼策略? – BuddyJoe 2009-08-14 16:51:29

回答

0

post顯示實現你在找什麼的一個很好的方式。

+0

立即嘗試此方法。謝謝。稍後再回來。 – BuddyJoe 2009-08-14 16:57:22

+0

鏈接不再有效 – 2012-06-04 16:43:08

0

什麼?

public ActionResult Details(int id, string format) { 
    if (!String.IsNullOrEmpty(format) && format == "xml") { 
    return View("MyView_Xml"); 
    } 
    else { 
    return View("MyView_Html"); 
    } 
} 

然後創建兩個視圖。 MyView_Xml:

<%@ Page Inherits="System.Web.Mvc.ViewPage<Customer>" ContentType="text/xml"> 
<?xml version="1.0" encoding="utf-8" ?> 
<customer> 
    <first_name><%= Model.FirstName %></first_name> 
    <last_name><%= Model.FirstName %></last_name> 
</customer> 

和MyView_Html

<%@ Page Inherits="System.Web.Mvc.ViewPage<Customer>"> 
<html> 
    <body> 
    <div><label>First Name:</label><%= Mode.FirstName %></div> 
    <div><label>Last Name:</label><%= Mode.LastName %></div> 
    </body> 
</html> 
+0

我看到這將幫助我序列化到XML,但如果我想爲Create()方法除了XML,該怎麼辦?好像我在這裏手寫更多的代碼。 – BuddyJoe 2009-08-14 16:49:48

+0

使用[AcceptVerbs(HttpVerbs.Post)] public ActionResult創建(XDocument xml){}接受XML帖子 – 2009-08-14 18:20:04