• MVCHelper 请求检验


        public class MVCHelper
        {
            //有 两 个ModelStateDictionary类,别弄混乱了要使用 System.Web.Mvc 下的
            //如果添加引用中找不到System.Web.MVC,那么可以用nuget安装:
            //Install-Package Microsoft.AspNet.Mvc
            public static string GetValidMsg(System.Web.Mvc.ModelStateDictionary modelState)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var key in modelState.Keys)
                {
                    if (modelState[key].Errors.Count <= 0)
                        continue;
    
                    sb.AppendFormat(" 属性【{0}】错误:", key);
                    foreach (var modelError in modelState[key].Errors)
                    {
                        sb.AppendLine(modelError.ErrorMessage);
                    }
                }
                return sb.ToString();
            }
            
            public static string RemoveQueryString(NameValueCollection nvc, string name)
            {
                NameValueCollection newNvc = new NameValueCollection(nvc);
                newNvc.Remove(name);
                return ToQueryString(newNvc);
            }
    
            public static string UpdateQueryString(NameValueCollection nvc, string name, string value)
            {
                NameValueCollection newNvc = new NameValueCollection(nvc);
                if (newNvc.AllKeys.Contains(name))
                    newNvc[name] = value;
                else
                    newNvc.Add(name, value);
    
                return ToQueryString(newNvc);
            }
    
            private static string ToQueryString(NameValueCollection newNvc)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var key in newNvc.AllKeys)
                {
                    string value = newNvc[key];
                    //EscapeDataString就是对特殊字符进行uri编码
                    sb.AppendFormat("{0}={1}&", key, Uri.EscapeDataString(value));
                }
                return sb.ToString().Trim('&');//去掉最后一个多余的&
            }
    
            //生成XXX的静态html页面的 方法
            public static string RenderViewToString(ControllerContext context, string viewPath, object model = null)
            {
                ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
                if (viewEngineResult == null)
                    throw new FileNotFoundException("View" + viewPath + "cannot be found.");
    
                var view = viewEngineResult.View;
                context.Controller.ViewData.Model = model;
                using (var sw = new StringWriter())
                {
                    var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
                    view.Render(ctx, sw);
                    return sw.ToString();
                }
            }
        }
  • 相关阅读:
    mysql 迁移
    ubuntu 安装 mysql
    Ubuntu 常用命令
    ubuntu 安装 redis
    Ubuntu 文件移动
    题解 [NOI2014] 动物园
    项目管理实践【四】Bug跟踪管理【Bug Trace and Management】
    项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
    项目管理实践【三】每日构建【Daily Build Using CruiseControl.NET and MSBuild】
    SQL Server修改表结构后批量更新所有视图
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/10825726.html
Copyright © 2020-2023  润新知