温故而知新集:项目总结-01某金融服务系统

3 篇文章 0 订阅
2 篇文章 0 订阅

开发环境:Visual Studio 2010 +SQL Sever 2008

应用技术:C# .NET,MVC,EF CodeFirst,存储过程,命令计划


功能与实现思路:

1、角色权限控制:使用MVC权限过滤器AuthorizeAttribute。在每一个需要权限控制的Action上打上Authorize特性或其子类的标签,并传递Roles参数,如“系统参数查看”只能是系统管理员(Role.Admin:Role类以及相关的角色名称自己配置,我这里是创建一个Role类存储系统当前的角色,用Admin表示系统管理员,用Approve表示审批员) 有权限查看,则相应的Action打上的Authorize标签的Roles属性应该为 [Authorize(Roles = "Admin")](允许两个以上角色访问,则用,隔开),重写Authorize下的AuthorizeCore方法,根据cookie获取当前登录用户的角色名称,通过Roles.Contains(role)==true?来判断当前登录角色用户是否有权限访问,有则return true,没有权限则跳出登录页面或其他操作。


2、excel的数据导入与报表导出:使用NPOI


3、操作日志记录:使用MVC动作过滤器特性ActionFilterAttribute,创建一个ActionLog类实现该特性,重写其中的OnResultExecuted方法,并添加自定义的属性如:Action字段等,在动作上打上ActionLog标签,传递自定义参数的值如,Action=“创建融资申请”等,OnResultExecuted方法中实现log数据的补充,并插入数据库或记事本。


4、邮件发送:

在webconfig中configuration节点的子节点appsettings中设置相关邮件发送参数

<configuration>
    <appSettings>
        <!--邮箱配置-->
        <add key="SmtpSendServer" value="smtp.qq.com"/>
        <add key="SmtpSendUsername" value="111111111@qq.com"/>
        <add key="SmtpSendPassword" value="123456"/>
        <add key="SmtpSendPort" value="25"/>
        <add key="SmtpSendSSL" value="false"/>
        <add key="SmtpSendFrom" value="2222222222@qq.com"/>
        <!--结束邮箱配置-->
    </appSettings>
</configuration>

创建EmailHelper类

using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Configuration;
using System.Net;


namespace Bbb.Core
{
    public class EmailHelper
    {
        //webconfig
        public static readonly string _smtpSendServer = System.Configuration.ConfigurationManager.AppSettings["SmtpSendServer"].ToString();
        public static readonly int    _smtpSendPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpSendPort"].ToString());
        public static readonly string _smtpSendUsername = System.Configuration.ConfigurationManager.AppSettings["SmtpSendUsername"].ToString();
        public static readonly string _smtpSendPassword = System.Configuration.ConfigurationManager.AppSettings["SmtpSendPassword"].ToString();
        public static readonly bool   _smtpSendSSL = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpSendSSL"].ToString());
        public static readonly string _smtpSendFrom = System.Configuration.ConfigurationManager.AppSettings["SmtpSendFrom"].ToString();


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="bcc">密件抄送人邮箱</param>
        /// <param name="cc">抄送人邮箱</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="message">输出信息</param>
        /// <returns>是否发送成功</returns>
        public static bool SendMailMessage(string to,string username, string subject, string body, out string message, string bcc = "", string cc = "")
        {
            bool flag = false;
            message = string.Empty;


            MailAddress fromAddress = new MailAddress(_smtpSendFrom);
            MailAddress toAddress = new MailAddress(to, username);
            MailMessage mailmessage = new MailMessage(fromAddress, toAddress);
            NetworkCredential networkCredential  = new NetworkCredential(_smtpSendFrom, _smtpSendPassword);
            if (!string.IsNullOrEmpty(bcc)) mailmessage.Bcc.Add(bcc);//密件抄送人邮箱
            if (!string.IsNullOrEmpty(cc)) mailmessage.CC.Add(cc);//抄送人邮箱


            //邮件设置
            mailmessage.Subject = subject;  //邮件标题 
            mailmessage.Body = body;        //邮件内容
            mailmessage.IsBodyHtml = true;  //指定邮件格式,支持HTML格式 
            mailmessage.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");//邮件采用的编码 
            mailmessage.Priority = MailPriority.High;//设置邮件的优先级为高(High/Low/Normal)


            //邮件服务器设定 
            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Host = _smtpSendServer; 
            client.EnableSsl = _smtpSendSSL;
            client.Port = _smtpSendPort;
            client.Timeout = 12000;
            client.Credentials = networkCredential;


            //发送邮件
            try
            {
                client.Send(mailmessage); 
                flag = true; 
                message = "发送成功!";
            }
            catch (SmtpFailedRecipientsException e)
            {
                flag = false;
                message = "无法发送邮件到所有邮件地址:" + e.Message;
            }
            catch (SmtpFailedRecipientException e)
            {
                flag = false;
                message = "无法发送邮件到个别邮件地址: " + e.Message;
            }
            catch (SmtpException e)
            {
                flag = false;
                message = "发送邮件时的Smtp异常: " + e.Message;
            }
            catch (Exception e)
            {
                flag = false;
                message = "发送邮件时的异常: " + e.Message;
            }
            finally
            {
                mailmessage.Dispose(); //释放资源  
            }


            return flag;
        }
    }
}

5、MVC视图免引用程序集,在view文件夹下的webconfig文件中的configuration节点的子节点->system.web.webPages.razor节点中统一引用,关闭重新生成。

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Routing" />
                <add namespace="PagedList" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

6、全局错误页面配置:在Global.asax.cs中的Application_Error方法实现,

protected void Application_Error(Object sender, EventArgs e)
        {
            //Exception lastError = Server.GetLastError();
            //string logPath = Server.MapPath("~/logs");
            //LogHelper logHelper = new LogHelper();
            //logHelper.LogError(lastError, logPath);

            var routeData = new RouteData();
            routeData.Values.Add("controller", "common");//--common/error的路由直接return View()一个错误页面,页面自定义
            routeData.Values.Add("action", "error");
            

            if (lastError is HttpRequestValidationException)
            {
                HttpContext.Current.Response.Write("请输入合法的字符串[<a href=\"javascript:history.back(0);\">返回</a>]");
                HttpContext.Current.Server.ClearError();
            }
            else
            {
                //throw new Exception(lastError.ToString());
                IController errorController = new CommonController();
                errorController.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData));
            }
        }

7、存储过程的调用,参考

            DbContext db = new DbContext();
            SqlParameter[] sqlParameter = new SqlParameter[10];
            //........
            decimal? sum = db.Database.SqlQuery<Nullable<Decimal>>("exec GetAllMargin_Proc @Status, @Id, @deaIds, @deaname, @isDeadLine, @beginDate, @number, @cnum, @outdate, @enddate", sqlParameter).First();




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值