用于json的 .ashx 小细节


public void ProcessRequest(HttpContext context)
{

          switch (action)

            {
                case "attribute_field_validate": //验证扩展字段是否重复
                    attribute_field_validate(context);
                    break;
                case "channel_category_validate": //验证频道分类目录是否重复
                    channel_category_validate(context);
                    break;case "get_builder_html": //生成静态页面
                    get_builder_html(context);
                    break;
            }
}

上面个用switch 来判断,一个语句就解决一个问题

下面是  Switch判断后 用来执行对应 函数

    #region 验证扩展字段是否重复============================
        private void attribute_field_validate(HttpContext context)
        {
            string column_name = MXRequest.GetString("param");
            if (string.IsNullOrEmpty(column_name))
            {
                context.Response.Write("{ "info":"名称不可为空", "status":"n" }");
                return;
            }
            BLL.article_attribute_field bll = new BLL.article_attribute_field();
            if (bll.Exists(column_name))
            {
                context.Response.Write("{ "info":"该名称已被占用,请更换!", "status":"n" }");
                return;
            }
            context.Response.Write("{ "info":"该名称可使用", "status":"y" }");
            return;
        }
        #endregion

这样优化一下,不但界面美观,容易理解->而且一个case一个功能

    
原文地址:https://www.cnblogs.com/0to9/p/4988107.html