MVC項目總結

來源:互聯網
上載者:User

標籤:關注   頁面   wim   single   format   substring   pat   html   video   

View命名

View下有多個模組的檔案夾,我們根據微軟的規定,每個模組下的首頁都為Index.cshtml命名

獲得當前頁面的控制器名稱

var currentControllerName = this.ViewContext.RouteData.Values["controller"].ToString();

獲得當前迴圈數組的index

@foreach (var banner in banners)
{

var index = banners.IndexOf(banner);

}

@using

如果view裡邊需要頻繁引入某個命名using XXX,我們選擇在_ViewImports.cshtml檔案中引入,相當於整個項目中都引用;

例如,在VR課堂項目中的該檔案我們添加以下兩行,整個項目中都可以使用:

@using vrlive.Models
@using vrlive.BLL

view直接存取BLL裡邊的倉庫檔案

view中可以直接存取BLL檔案夾下的倉庫檔案,來訪問資料庫,獲得所需的資訊,方法如下:

  1. 在view中使用inject引入所需倉庫檔案@inject BannerRepository BannerRepository
  2. 獲得該倉庫下的資訊:var banners = BannerRepository.GetBannerList();
  3. 使用變數banners,即可得到GetBannerList()函數下返回的資料;
定義數組
  • new string[] { "大一課程", "大二課程", "大三課程", "大四課程" }
  • string[][] orderType = new string[][] { new[] { "最新", "AddTime" }, new[] { "熱門", "VisitCount" }, new[] { "關注量", "FavoriteCount" } };
類型轉換
  • Convert.ToInt32(ViewData["pageNo"]): 將object轉換為number類型
  • ViewData["currentVideoPath"] as string:將object轉換為string類型
  • Convert.ToBoolean(ViewData["currentVideoIsLive"]):將object轉換為bool類型
擷取當前url
  • 擷取當前url的參數:
    @using Microsoft.Extensions.Primitives

    StringValues videoId;
    this.ViewContext.HttpContext.Request.Query.TryGetValue("singleVideoId", out videoId);
    var singleVideoId = videoId.ToString();

  • 擷取當前的url包括參數
    string url = this.ViewContext.HttpContext.Request.Path + this.ViewContext.HttpContext.Request.QueryString; 

跳轉至登入頁面時,有多個參數無法return問題
Uri.UnescapeDataString(Request.QueryString.Value);是解決擷取當前頁面的url沒有解析的問題。
 public IActionResult Login(string returnUrl)        {            var queryString = Uri.UnescapeDataString(Request.QueryString.Value);            int index = queryString.IndexOf(returnUrl);            string realReturnUrl = "";            if (index > 0)            {                realReturnUrl = queryString.Substring(index);            }            else            {                realReturnUrl = queryString;            }            ViewData["ReturnUrl"] = realReturnUrl;            //ViewData["ReturnUrl"] = returnUrl;            return View();        }

 

MVC項目總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.