各种内容输出方法的用法

1 输出JavaScript

  public ActionResult JavaScriptTest()
    {
        string js = "alert('Welecome to ASP.NET MVC!')";
        return JavaScript(js);
    }

2 输出Json

 public ActionResult JsonTest()
    {
        var book = new
        {
            bookid = 1,
            bookName = "精通ASP.NET MVC",
            author = "作者",
            publishData = DateTime.Now
        };
        return Json(book, JsonRequestBehavior.AllowGet);
    }

3 链接文件 播放MP3

  public ActionResult FilePathTest()
    {
        return File("~/Content/rain.mp3", "audio/mp3");
    }

4 链接文件内容

 public ActionResult FileContentTest()
    {
        string content = "Welcome to ASP.NET MVC!";
        byte[] contents = System.Text.Encoding.UTF8.GetBytes(content);

        return File(contents, "text/plain");
    }

5 链接文件流

 public ActionResult FileStreamTest()
    {
        string content = "Welcome to ASP.NET MVC!";
        byte[] contents = System.Text.Encoding.UTF8.GetBytes(content);

        FileStream fs = new FileStream(
            Server.MapPath("~/Content/Telerik RadControls for ASP.NET AJAX (Custom).pdf"), FileMode.Open);
        return File(fs, "application/pdf");
    }

6 输出文本

 public ActionResult ContentTest()
    {
        string content = "<h1>Welcome to ASP.NET MVC!</h1>";
        return Content(content);
    }

7 跳转(Redirect)

public ActionResult RedirectTest()
    {
        return Redirect("/Home/Detail");
    }

RedirectToRoute跳转

  public ActionResult RedirectToRouteTest()
    {
        return RedirectToRoute(new { controller = "Home", action = "Detail",
            id = 1, cate = "test" });
    }

RedirectToAction跳转

 public ActionResult RedirectToActionTest()
    {
        return RedirectToAction("Detail", new { id = 1, cate = "test" });
    }

 

posted @ 2020-10-07 19:45  小九家的丫头  阅读(430)  评论(0编辑  收藏  举报