代码之家  ›  专栏  ›  技术社区  ›  mynameiscoffey

在asp.net mvc中,是否有一个等价于ruby on rails对format.xml等的响应?

  •  11
  • mynameiscoffey  · 技术社区  · 15 年前

    在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 从视图返回HTML页面 index.html.erb (思考index.aspx)
    http://example.com/Movies/Index.xml 以XML格式返回相同的数据( @movies 是包含所有视图使用的数据的对象)
    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中有没有类似的方法呢?

    3 回复  |  直到 15 年前
        1
  •  8
  •   Mara Morton    15 年前

    在我的博客中,我详细介绍了一种处理方法,它的功能与ruby on rails中的功能非常相似。您可以在文章的底部找到链接,但这里是最终结果的示例:

    public ActionResult Index()
    {
        return RespondTo(format =>
        {
            format.Html = () => View();
            format.Json = () => Json(new { message = "hello world" });
        });
    }
    

    以下是帖子的链接: http://icanhascode.com/2009/05/simple-ror-respond_to-functionality-in-aspnet-mvc/

    它可以通过http头和路由中的变量来检测正确的类型。

        2
  •  6
  •   mynameiscoffey    15 年前

    所以我一直在玩这个,并添加了以下到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 = "" });  
    

    现在每当我需要一个控制器动作来“格式感知”时,我只需添加一个 string format 论证(例如):

    // Within Home Controller
    public ActionResult MovieList(string format)
    {
        List<Movie> movies = CreateMovieList();
    
        if ( format == "json" )
            return Json(movies);
    
        return View(movies);
    }
    

    现在当我打电话 /Home/MovieList 它将一如既往地返回标准html视图,如果我调用 /Home/MovieList.json 它返回传递到视图中的相同数据的json序列化字符串。这将适用于您碰巧使用的任何视图模型,我使用一个非常简单的列表只是为了修补。

    为了让事情变得更好,您甚至可以在视图中执行以下操作:

    链接到 /家庭/电影制作人
    <%= Html.ActionLink("Test", "MovieList") %>

    链接到 /home/movielist.json主页
    <%= Html.ActionLink("JSON", "MovieList", new { format = "json" }) %>

        3
  •  2
  •   Eilon    15 年前

    在ASP.NET MVC中没有对此的内置支持。但是,您可以下载一个示例rest工具包:

    阅读有关上的rest工具包的更多信息 Phil's blog .

    rest工具包有“格式提供者”,它们为各种请求定义结果类型。指导文档可在asp.net mvc 1.0的下载中找到。以下是指导文件的摘录:

    这个控制器现在可以返回xml或json作为对http get请求的响应。格式是根据请求的内容类型或请求的接受头中的内容类型确定的。