zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
  • 相关阅读:
    [原创]Windows 7 下成功添加网络共享HP打印机
    [原创]PDFCreator自动保存及文件名带空格、后缀名丢失的解决方法(Windows 7通过)
    [原创]U872客户端“system.net.sockets.socketexception”的解决方法
    [转载]Windows 7默认共享无法访问
    [转载]使Excel不显示0值的三招
    [原创]使用空密码远程桌面连接
    分布式架构理论
    ffmpeg重要函数和结构体整理
    es~存储部分字段
    es~text与keyword的选择
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/10825726.html
Copyright © 2011-2022 走看看