是否有相当于 Ruby on Rails 的工具? ASP.Net MVC 中的 respond_to format.xml 等?
在 Ruby on Rails 中,您可以编写一个简单的控制器操作,例如:
def index
@movies = Movies.find(:all)
respond_to do |format|
format.html #index.html.erb
format.xml { render :xml => @movies }
format.json { render :json => @movies }
end
end
对于那些不熟悉 RoR 的人,在本例中 def index
相当于 public ActionResult Index()
ASP.Net MVC 控制器,并允许以下调用:
http://example.com/Movies/Index
从视图 index.html.erb
作为 html 页面返回>(想想index.aspx)http://example.com/Movies/Index.xml
以 xml 格式返回相同的数据(@movies
是包含所有视图使用的数据的对象)< br> http://example.com/Movies/Index.json
返回一个 JSON 字符串,在进行需要相同数据/逻辑的 javascript 调用时很有用
ASP.Net MVC 中的等效流程(如果可能)可能会看起来像这样(如果它可以不那么冗长,甚至更好):
public ActionResult Index()
{
Movies movies = dataContext.GetMovies();
// any other logic goes here
switch (format)
{
case "xml":
return View("XMLVIEW");
break;
case "json":
return View("JSONVIEW");
break;
default:
return View();
}
}
这真的很方便,不必让一堆不同的操作弄乱你的控制器,有没有办法在 ASP.Net MVC 中做类似的事情?
In Ruby on Rails you can write a simple controller action such as:
def index
@movies = Movies.find(:all)
respond_to do |format|
format.html #index.html.erb
format.xml { render :xml => @movies }
format.json { render :json => @movies }
end
end
For those unfamiliar with RoR, def index
in this case would be the equivalent of public ActionResult Index()
within an ASP.Net MVC Controller and would allow the following calls:
http://example.com/Movies/Index
returns as an html page from the view index.html.erb
(think index.aspx)http://example.com/Movies/Index.xml
returns the same data in xml format (@movies
is the object containing the data all of the views use)http://example.com/Movies/Index.json
returns a JSON string, useful when making javascript calls needing the same data/logic
An equivalent flow in ASP.Net MVC would (if possible) likely look something like this (if it could be less verbose, even better):
public ActionResult Index()
{
Movies movies = dataContext.GetMovies();
// any other logic goes here
switch (format)
{
case "xml":
return View("XMLVIEW");
break;
case "json":
return View("JSONVIEW");
break;
default:
return View();
}
}
This is really handy not having to keep a bunch of different actions cluttering up your controller, is there a way to do something similar in ASP.Net MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我的博客上,我详细介绍了一种处理此问题的方法,该方法的功能与 Ruby on Rails 中的功能非常相似。您可以在帖子底部找到该链接,但这里是最终结果的示例:
这是帖子的链接:http://icanhascode.com/2009/05/simple-ror-respond_to-functionity-in-aspnet-mvc/
可以处理通过 HTTP 标头以及路由中的变量检测正确的类型。
On my blog, I've detailed a method of handling this that functions in a very similar way to how it functions in Ruby on Rails. You can find the link at the bottom of the post, but here is a sample of the end result:
Here's the link to the post: http://icanhascode.com/2009/05/simple-ror-respond_to-functionality-in-aspnet-mvc/
It can handle detecting the correct type via HTTP headers as well as variables in the route.
因此,我一直在研究这个问题,并向 RegisterRoutes() 添加了以下路由:
现在,每当我需要控制器操作“格式感知”时,我只需向其添加一个
string format
参数(例如):现在,当我调用
/Home/MovieList
时,它会一如既往地返回标准 html 视图,如果我调用/Home/MovieList.json
,它会返回一个 JSON 序列化传递到视图中的相同数据的字符串。这适用于您碰巧使用的任何视图模型,我使用一个非常简单的列表只是为了修补。为了让事情变得更好,您甚至可以在视图中执行以下操作:
链接到
/Home/MovieList
<%= Html.ActionLink("Test", "MovieList") %>
链接到
/Home/MovieList.json
<%= Html.ActionLink("JSON", "MovieList", new { format = "json" }) %>
So I've been playing with this and added the following routes to RegisterRoutes():
Now whenever I need a Controller Action to be "format aware" I simply add a
string format
argument to it (such as):Now when I call
/Home/MovieList
it returns the standard html view as always and if I make a call to/Home/MovieList.json
it returns a JSON serialized string of the same data passed into the view. This will work for any view model you happen to be using, I'm using a very simple list just for the sake of tinkering.To make things even better you can even do the following within the views:
Links to
/Home/MovieList
<%= Html.ActionLink("Test", "MovieList") %>
Links to
/Home/MovieList.json
<%= Html.ActionLink("JSON", "MovieList", new { format = "json" }) %>
ASP.NET MVC 中没有对此的内置支持。不过,您可以下载一个示例 REST 工具包:
有关 REST 工具包的更多信息,请访问 Phil 的博客。
REST 工具包具有“格式提供程序”,用于定义各种请求的结果类型。该指导文档可在 ASP.NET MVC 1.0 的下载中找到。以下是指导文件的摘录:
There is no built-in support for this in ASP.NET MVC. However, there is a sample REST toolkit that you can download:
Read more about the REST toolkit on Phil's blog.
The REST toolkit has "format providers" that define result types for various requests. The guidance document is available in the download for ASP.NET MVC 1.0. Here's an excerpt from the guidance document: