MVC字符串处理及MVC @RenderSection小计

   最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

   十年河东十年河西,莫欺少年穷

   学无止境,精益求精

   最近在做自学MVC,遇到的问题很多,索性一点点总结下。

   新建一个非空的MVC项目,我们在查看_Layout.cshtml时,会发现@RenderSection(),@Styles.Render()等标签,那么这些标签的作用是什么呢?

   _Layout.cshtml 代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="">
    <meta name="format-detection" content="telephone=no">
    <meta id="viewport" name="viewport" content="width=750px,minimum-scale=0.5,maximum-scale=5,user-scalable=no" />
    <title>@ViewBag.Title</title>
    <!--用于在App_Start/BundleConfig.cs中绑定系统所需的通用CSS-->
    @Styles.Render("~/Content/css")
    <!--用于子页面中绑定特有CSS-->
    <!--在子页面中用法:@section css{<link href="~/Content/login.css" rel="stylesheet" />}-->
    @RenderSection("css", required: false)
</head>
<body>
    <!--Body标签中所含的HTML脚本,如:<Form></Form>-->
    @RenderBody()
    <!--用于在App_Start/BundleConfig.cs中绑定系统所需的通用JS、Jquery-->
    @Scripts.Render("~/bundles/jquery")
    <!--用于子页面中绑定特有JS、JQuery-->
    <!--在子页面中用法:@section script{<script href="~/Content/login.js"></script>}-->
    @RenderSection("scripts", required: false)
</body>
</html>

   子页面中设定特定的CSS或JS

@{
    ViewBag.Title = "登录";
}
@section css
{
    <link href="~/Content/login.css" rel="stylesheet" />
}

   BundleConfig.cs 中代码如下

    public class BundleConfig
    {
        // 有关 Bundling 的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-1.11.1.min.js",
                        "~/Scripts/TouchSlide.1.1.js",
                         "~/Scripts/jquery.Slide.js",
                          "~/Scripts/jquery.inputbox.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/common.css"));

            
        }
    }

   以上便是上述标签的用法,好记性不如烂笔头,索性记录下来

   补充(2016-11-13)

   C#字符串处理的过程中经常会碰到中英文数字等组合的字符串,其中中文字符每个站位为2,英文 数字等站位为1,在截取的过程中不好处理,下面来介绍一种处理中英文字符混合的方法,如下:

#region 截取字符长度 static string CutString(string str, int len)
        /// <summary>
        /// 截取字符长度
        /// </summary>
        /// <param name="str">被截取的字符串</param>
        /// <param name="len">所截取的长度</param>
        /// <returns>子字符串</returns>
        public static string CutString(string str, int len)
        {
            if (str == null || str.Length == 0 || len <= 0)
            {
                return string.Empty;
            }

            int l = str.Length;


            #region 计算长度
            int clen = 0;
            while (clen < len && clen < l)
            {
                //每遇到一个中文,则将目标长度减一。
                if ((int)str[clen] > 128) { len--; }
                clen++;
            }
            #endregion

            if (clen < l)
            {
                return str.Substring(0, clen) + "...";
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// //截取字符串中文 字母
        /// </summary>
        /// <param name="content">源字符串</param>
        /// <param name="length">截取长度!</param>
        /// <returns></returns>
        public static string SubTrueString(object content, int length)
        {
            string strContent = NoHTML(content.ToString());

            bool isConvert = false;
            int splitLength = 0;
            int currLength = 0;
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            for (int i = 0; i < strContent.Length; i++)
            {
                code = Char.ConvertToUtf32(strContent, i);
                if (code >= chfrom && code <= chend)
                {
                    currLength += 2; //中文
                }
                else
                {
                    currLength += 1;//非中文
                }
                splitLength = i + 1;
                if (currLength >= length)
                {
                    isConvert = true;
                    break;
                }
            }
            if (isConvert)
            {
                return strContent.Substring(0, splitLength);
            }
            else
            {
                return strContent;
            }
        }

        public static int GetStringLenth(object content)//获取混合字符串长度,根据长度进行长度/前2...后2等方式截取
        {
            string strContent = NoHTML(content.ToString());
            int currLength = 0;
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            for (int i = 0; i < strContent.Length; i++)
            {
                code = Char.ConvertToUtf32(strContent, i);
                if (code >= chfrom && code <= chend)
                {
                    currLength += 2; //中文
                }
                else
                {
                    currLength += 1;//非中文
                }
               
            }
            return currLength;
        }
        #endregion

   @陈卧龙的博客

   

posted @ 2016-11-12 11:33  天才卧龙  阅读(1424)  评论(0编辑  收藏  举报