asp.net MVC 三 ActionResult返回其他值

來源:互聯網
上載者:User

一、返回.ascx頁面

   1、還是建立一個Action

 

     public ActionResult Ascx()
        {
            return PartialView();//返回部分視圖
        }

   2、建立一個View檔案

    

然後通過http://localhost:4116/admin/ascx訪問顯示結果。

二、返迴文本內容

    public ActionResult Text()
        {
            return Content("這是一段文字");
        }

三、返回一個序列化的結果Json

     public ActionResult ShowJson()
        {
            MvcApplication2.Models.Member m = new MvcApplication2.Models.Member();
            m.name = "張三";
            m.sex = "男";
            m.age = "24";
            return Json(m);
        }

      輸出結果{"Name":"鄒健","Sex":true}註:這個未驗證

四、輸出JS檔案

大多時候js檔案都是靜態,但有時js檔案可能也要動態產生這時我們可以這樣輸出

        public ActionResult Js()        {            return JavaScript("var x=0;");        }

我們訪問之,得到一個正常頁面但其Content-Type:application/x-javascript; charset=utf-8

五、頁面跳轉

1.跳轉到Url

        public ActionResult rdurl()        {            return Redirect("http://www.baidu.com");        }

2.跳轉到Action

        public ActionResult rdaction()        {            return RedirectToAction("Index","Eice");        }

3.跳轉到Routing規則

        public ActionResult rdrouting()        {            return RedirectToRoute("Default",//Route名              new{                  Controller = "Eice",                  Action = "Index"              });        }
六、顯示檔案
        public ActionResult fn()        {            return File(                "/Content/site.css"//檔案路徑                , "text/css"//檔案類型                );        }

上一篇:ASP.NET MVC 二 ActionResult傳回值

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.