是否有一个相当于Ruby on Rails的'的respond_to format.xml等在ASP.Net MVC? [英] Is there an equivalent to Ruby on Rails' respond_to format.xml, etc in ASP.Net MVC?

查看:186
本文介绍了是否有一个相当于Ruby on Rails的'的respond_to format.xml等在ASP.Net MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby on Rails的,你可以写一个简单的控制器操作,如:

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

有关在这种情况下,那些不熟悉的回报率, DEF指数将相当于公众的ActionResult指数()一个ASP.Net MVC控制器内并允许以下呼叫:

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 返回从查看HTML页面 index.html.erb (认为的Index.aspx)结果
http://example.com/Movies/Index.xml 以XML格式返回( @movies 是相同的数据包含所有的视图使用的数据对象)的结果
http://example.com/Movies/Index.json 返回一个JSON字符串,有用使得当JavaScript调用需要相同的数据/逻辑

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

在ASP.Net MVC的等效流量会(如果可能的话)可能是这个样子(如果它可能是更简洁,甚至更好):

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();
    }
}

这是非常方便的不必保持了一堆不同的动作搞乱控制器的,有没有办法做同样在ASP.Net MVC的东西吗?

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?

推荐答案

所以,我一直在玩这个,并增加了以下路线的RegisterRoutes():

So I've been playing with this and added the following routes to RegisterRoutes():

routes.MapRoute("FormatAction", "{controller}/{action}.{format}",
                new { controller = "Home", action = "Index" });  

routes.MapRoute("FormatID", "{controller}/{action}/{id}.{format}",
                new { controller = "Home", action = "Index", id = "" });  

现在,每当我需要一个控制器动作被格式化意识到我只需要添加一个字符串格式参数把它(如):

Now whenever I need a Controller Action to be "format aware" I simply add a string format argument to it (such as):

// Within Home Controller
public ActionResult MovieList(string format)
{
    List<Movie> movies = CreateMovieList();

    if ( format == "json" )
        return Json(movies);

    return View(movies);
}

现在,当我打电话 /主页/ MovieList 返回标准的HTML视图一如既往,如果我做出 /主页/ MovieList打电话。 JSON 返回传递到视图中的同一数据的JSON序列串。这适用于任何浏览模式工作,你碰巧使用的,我用一个很简单的列表只是修修补补的缘故。

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:

链接 /主页/ MovieList 结果
&LT;%= Html.ActionLink(测试,MovieList)%&GT;

链接 /Home/MovieList.json 结果
&LT;%= Html.ActionLink(JSON,MovieList,新{格式=JSON})%&GT;

这篇关于是否有一个相当于Ruby on Rails的'的respond_to format.xml等在ASP.Net MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆