e媒网络

一切皆可能 e媒网络 http://www.eMay.net

博客园 首页 新随笔 联系 订阅 管理

1.强制xml输出格式转换成Json:

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
        }

在Applicaiton_Start中的下方添加:

 GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

 

2.通过身份证,计算 生日、性别、年龄:

提示:8位的身份证,前面六位代表了你户籍所在地,第七位到第十四位代表了你的出生年月,第十五位到第十七为代表了你的性别(偶数为女,奇数为男),根据这一信息,我在系统开发的录入员工的身份证后控件焦点转移时根据身份证号码获得生日和性别。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdentityCardDemo
{
    class Program
    {
         static int CalculateAge(string birthDay)
        {
            DateTime birthDate = DateTime.Parse(birthDay);
            DateTime nowDateTime = DateTime.Now;
            int age = nowDateTime.Year - birthDate.Year;
            //再考虑月、天的因素
            if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
            {
                age--;
            }
            return age;
        }

        static void Main(string[] args)
        {
           
            string identityCard = "610121197610185973";
            string birthday = "";
            string sex = "";
            int age = 0;

            if (identityCard.Length == 18)
            {
                birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
                sex = identityCard.Substring(14, 3);
            }
            //处理15位的身份证号码从号码中得到生日和性别代码
            if (identityCard.Length == 15)
            {
                birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
                sex = identityCard.Substring(12, 3);
            }
           
            //性别代码为偶数是女性奇数为男性
            if (int.Parse(sex) % 2 == 0)
            {
                sex = "";
            }
            else
            {
                sex = "";
            }
            age = CalculateAge(birthday);
            Console.WriteLine(birthday);
            Console.WriteLine(sex);
            Console.WriteLine(age);

        }
    }
}

 3.Model参考代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace IDCardDemo.Models
{
    public class IDCardV2M
    {
        private string num;
        private string birthday;     
        private string sex;
        private int age;

        [Required]
        //[StringLength(18,ErrorMessage="身份证号码位数为18位", MinimumLength =18)]
        [RegularExpression(@"(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$)", ErrorMessage = "格式不正确")]
        [Display(Name ="身份证号码")]
        public string Num { get 
            {
                return num;
            }
            set {
                num = value;
            } }   
        public string Birthday
        {
            get
            {
                if (string.IsNullOrEmpty(num))
                    return "";

                if (num.Length == 18)
                {
                    birthday = num.Substring(6, 4) + "-" + num.Substring(10, 2) + "-" + num.Substring(12, 2);
                }
                else if (num.Length == 15)
                {
                    birthday = "19" + num.Substring(6, 2) + "-" + num.Substring(8, 2) + "-" + num.Substring(10, 2);

                }
                else
                    birthday = "";
                return birthday;
            }
        }     
        public int Age
        {

            get
            {
                if (string.IsNullOrEmpty(num))
                    return -1;

                if (num.Length == 18)
                {
                    birthday = num.Substring(6, 4) + "-" + num.Substring(10, 2) + "-" + num.Substring(12, 2);
                }
                else if (num.Length == 15)
                {
                    birthday = "19" + num.Substring(6, 2) + "-" + num.Substring(8, 2) + "-" + num.Substring(10, 2);

                }
                else
                {
                    birthday = "";
                    return -1;
                }

                DateTime birthDate = DateTime.Parse(birthday);
                DateTime nowDateTime = DateTime.Now;
                int agee = nowDateTime.Year - birthDate.Year;
                //再考虑月、天的因素
                if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
                {
                    agee--;
                }
                return agee;
            }
        }
        public int Length
        {
            get
            {
                if (string.IsNullOrEmpty(num))
                    return -1;
                if (num.Length == 15 || num.Length == 18)
                    return num.Length;
                else
                    return -1;
            }
        }
        public string Gender
        {
            get
            {
                if (string.IsNullOrEmpty(num))
                    return "";
                if (num.Length == 18)
                {

                    sex = num.Substring(14, 3);

                }
                else if (num.Length == 15)
                {
                    sex = num.Substring(12, 3);
                }
                else
                    sex = "0";



                if (int.Parse(sex) % 2 == 0 && sex != "0")
                {
                    sex = "";
                }
                else if (sex != "0")
                {
                    sex = "";
                }
                else
                    sex = "";

                return sex;
            }
        }

    }
}

4. 控制器参考代码:

using IDCardDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IDCardDemo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        int CalculateAge(string birthDay)
        {
            DateTime birthDate = DateTime.Parse(birthDay);
            DateTime nowDateTime = DateTime.Now;
            int age = nowDateTime.Year - birthDate.Year;
            //再考虑月、天的因素
            if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
            {
                age--;
            }
            return age;
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        [HttpGet]
        public ActionResult  IDCard()
        {            

            return View();
        }
        [HttpGet]
        public ActionResult IDCardV2()
        {

            return View();
        }
        [HttpPost]
        public ActionResult IDCard(string identityCard)
        {

            //string identityCard = "610121197610185973";
            string birthday = "";
            string sex = "";
            int age = 0;

            if (identityCard.Length == 18)
            {
                birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
                sex = identityCard.Substring(14, 3);
            }
            //处理15位的身份证号码从号码中得到生日和性别代码
            if (identityCard.Length == 15)
            {
                birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
                sex = identityCard.Substring(12, 3);
            }

            //性别代码为偶数是女性奇数为男性
            if (int.Parse(sex) % 2 == 0)
            {
                sex = "";
            }
            else
            {
                sex = "";
            }
            age = CalculateAge(birthday);
            //Console.WriteLine(birthday);
            //Console.WriteLine(sex);
            Console.WriteLine(age);
            ViewBag.birthday = birthday;
            ViewBag.age = age.ToString();
            ViewBag.sex = sex;

            return View();
        }

        [HttpPost]
        public ActionResult IDCardV2(IDCardV2M box)
        {
            int a = 12;
            a += 5;

            return View("Details",box);
        }


        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

5.View视图参考代码:

@model IDCardDemo.Models.IDCardV2M

@{
    ViewBag.Title = "IDCardV2";
}

<h2>IDCardV2</h2>

@using (Html.BeginForm("IDCardV2", "Home", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>IDCardV2</h4>
        <p>比如输入:610121197610185973.  320311770706001</p>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Num, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Num, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Num, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
@section Scripts
 {
    @Scripts.Render("~/bundles/jqueryval")
 }
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

 switch语句新用法:

using System;

namespace SwitchDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal m =-2M;
            switch(m)
            {
                case  decimal when m< 60&&m>=0:
                    Console.Write("No Pass");
                    break;
                case  decimal when m<=100&&m>=60:
                    Console.Write("Pass");
                    break;
                default:
                    Console.Write(">100或者<0");
                    break;
            }
         
        }
    }
}

 switch升级版:

using System;

namespace SwitchDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //object m ="B";
            //object m = 'B';
            //object m=88;
            object m = 88M;
            switch (m)
            {
                case  decimal n when n< 60&&n>=0:
                    Console.Write("No Pass");
                    break;
                case  decimal n when n<=100&&n>=60:
                    Console.Write("Pass");
                    break;
                case char n when n == 'A':
                    Console.Write("Best");
                    break;
                case char n when n == 'B':
                    Console.Write("Good");
                    break;
                case char n when n == 'C':
                    Console.Write("Bad");
                    break;
                default:
                    Console.Write("非法数据");
                    break;
            }
         
        }
    }
}

 

posted on 2022-03-24 08:33  e媒网络技术团队  阅读(28)  评论(0编辑  收藏  举报