博客 (93)

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace xxx.xxx.xxx.Controllers
{
    public class DiscuzTinyintViewerController : Controller
    {
        public IActionResult Index()
        {
            using var context = new Data.xxx.xxxContext();

            var conn = context.Database.GetDbConnection();
            conn.Open();
            using var cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT `TABLE_NAME` FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE();";
            Dictionary<string, List<FieldType>?> tables = new();

            using var r = cmd.ExecuteReader();
            while (r.Read())
            {
                tables.Add((string)r["TABLE_NAME"], null);
            }
            conn.Close();

            foreach (var table in tables)
            {
                var conn2 = context.Database.GetDbConnection();
                conn2.Open();
                using var cmd2 = conn2.CreateCommand();
                cmd2.CommandText = "DESCRIBE " + table.Key;
                using var r2 = cmd2.ExecuteReader();
                List<FieldType> fields = new();
                while (r2.Read())
                {
                    if (((string)r2[1]).Contains("tinyint(1)"))
                    {
                        fields.Add(new()
                        {
                            Field = (string)r2[0],
                            Type = (string)r2[1],
                            Null = (string)r2[2],
                        });
                    }
                }
                conn2.Close();
                tables[table.Key] = fields;
            }

            foreach (var table in tables)
            {
                foreach (var f in table.Value)
                {
                    var conn3 = context.Database.GetDbConnection();
                    conn3.Open();
                    using var cmd3 = conn3.CreateCommand();
                    cmd3.CommandText = $"SELECT {f.Field} as F, COUNT({f.Field}) as C FROM {table.Key} GROUP BY {f.Field}";
                    using var r3 = cmd3.ExecuteReader();
                    List<FieldType.ValueCount> vs = new();
                    while (r3.Read())
                    {
                        vs.Add(new() { Value = Convert.ToString(r3["F"]), Count = Convert.ToInt32(r3["C"]) });
                    }
                    conn3.Close();
                    f.groupedValuesCount = vs;
                }
            }

            return Json(tables.Where(c => c.Value != null && c.Value.Count > 0));
        }

        private class FieldType
        {
            public string Field { get; set; }
            public string Type { get; set; }
            public string Null { get; set; }
            public List<ValueCount> groupedValuesCount { get; set; }
            public class ValueCount
            {
                public string Value { get; set; }
                public int Count { get; set; }
            }
            public string RecommendedType
            {
                get
                {
                    if (groupedValuesCount == null || groupedValuesCount.Count < 2)
                    {
                        return "无建议";
                    }
                    else if (groupedValuesCount.Count == 2 && groupedValuesCount.Any(c => c.Value == "0") && groupedValuesCount.Any(c => c.Value == "1"))
                    {
                        return "bool" + (Null == "YES" ? "?" : "");
                    }
                    else
                    {
                        return "sbyte" + (Null == "YES" ? "?" : "");
                    }
                }
            }
        }
    }
}
[{
	"key": "pre_forum_post",
	"value": [{
		"field": "first",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1395501
		}, {
			"value": "1",
			"count": 179216
		}],
		"recommendedType": "bool"
	}, {
		"field": "invisible",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "-5",
			"count": 9457
		}, {
			"value": "-3",
			"count": 1412
		}, {
			"value": "-2",
			"count": 1122
		}, {
			"value": "-1",
			"count": 402415
		}, {
			"value": "0",
			"count": 1160308
		}, {
			"value": "1",
			"count": 3
		}],
		"recommendedType": "sbyte"
	}, {
		"field": "anonymous",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1574690
		}, {
			"value": "1",
			"count": 27
		}],
		"recommendedType": "bool"
	}, {
		"field": "usesig",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 162487
		}, {
			"value": "1",
			"count": 1412230
		}],
		"recommendedType": "bool"
	}, {
		"field": "htmlon",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1574622
		}, {
			"value": "1",
			"count": 95
		}],
		"recommendedType": "bool"
	}, {
		"field": "bbcodeoff",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "-1",
			"count": 935448
		}, {
			"value": "0",
			"count": 639229
		}, {
			"value": "1",
			"count": 40
		}],
		"recommendedType": "sbyte"
	}, {
		"field": "smileyoff",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "-1",
			"count": 1359482
		}, {
			"value": "0",
			"count": 215186
		}, {
			"value": "1",
			"count": 49
		}],
		"recommendedType": "sbyte"
	}, {
		"field": "parseurloff",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1572844
		}, {
			"value": "1",
			"count": 1873
		}],
		"recommendedType": "bool"
	}, {
		"field": "attachment",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1535635
		}, {
			"value": "1",
			"count": 2485
		}, {
			"value": "2",
			"count": 36597
		}],
		"recommendedType": "sbyte"
	}, {
		"field": "comment",
		"type": "tinyint(1)",
		"null": "NO",
		"groupedValuesCount": [{
			"value": "0",
			"count": 1569146
		}, {
			"value": "1",
			"count": 5571
		}],
		"recommendedType": "bool"
	}]
}]


xoyozo 3 年前
1,994

POST

axios.post('AjaxCases', {
    following: true,
    success: null,
    page: 1,
}).then(function (response) {
    console.log(response.data)
}).catch(function (error) {
    console.log(error.response.data);
});
[HttpPost]
public IActionResult AjaxCases([FromBody] AjaxCasesRequestModel req)
{
    bool? following = req.following;
    bool? success = req.success;
    int page = req.page;
}

GET

axios.get('AjaxCase', {
    params: {
        int: 123,
    }
}).then(function (response) {
    console.log(response.data)
}).catch(function (error) {
    console.log(error.response.data);
});
[HttpGet]
public IActionResult AjaxCases([FromQuery] int id)
{
}

DELETE

格式与 GET 类似


* 若服务端获取参数值为 null,可能的情况如下:

  • 请检查相关枚举类型是否已设置 [JsonConverter] 属性,参:C# 枚举(enum)用法笔记,且传入的值不能为空字符串,可以传入 null 值,并将服务端 enum 类型改为可空。可以以 string 方式接收参数后再进行转换。

  • 模型中属性名称不能重复(大小写不同也不行)。

  • 布尔型属性值必须传递布尔型值,即在 JS 中,字符串 "true" 应 JSON.parse("true") 为 true 后再回传给服务端。


参考:jQuery 请求 .NET Core / .NET 5 / .NET 6

xoyozo 3 年前
3,253

直接运行项目启动程序(在发布目录中,以项目名称为文件名,扩展名为 .exe 的可执行程序)

启动后可在“Now listening on:”所在行看到访问入口 URL(如图中 http://localhost:5000)。

在同服浏览器中打开该地址,访问到报错页面,回到该命令行窗口可见“fail”异常的详细信息。

QQ截图20211003173208.png

xoyozo 4 年前
2,283

打开文件 Startup.cs,找到:

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));

将这两行移出 if 外。

xoyozo 4 年前
4,964

当 ASP.NET 发布一个新的小版本更新时(以 .NET 5.0.10 为例),生产环境服务器端可以直接下载安装 .NET 5.0.10 来自动替换 .NET 5.0.9。

倘若这时直接使用 VS 将 nuget 中版本为 5.0.9 的组件更新到 5.0.10 发布到服务器上的话,可能会发生错误:

HTTP Error 500.30 - ASP.NET Core app failed to start

这里我们可以在 nuget 回滚相关组件的版本至 5.0.9,等 VS 发现新的版本更新时,更新 VS 开发工具后,再更新 nuget 组件并进行发布即可。

着急的小伙伴可以直接前往 Microsoft .NET 网站下载安装包含 Runtime 5.0.10 的 SDK:

image.png

该情况同样发生在 ASP.NET Core 3.1 等大版本上。

若仍未生效,请重启服务器

----------------

后注:安装完成后重启服务器可避免以上烦恼!

xoyozo 4 年前
2,014
private static List<string> GetTableNames(DbContext context)
{
    context.Database.OpenConnection();

    var connection = context.Database.GetDbConnection();

    using var command = connection.CreateCommand();
    command.CommandText = @"SELECT `TABLE_NAME`
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE();";
    var tableNames = new List<string>();

    using var reader = command.ExecuteReader();
    while (reader.Read())
    {
        tableNames.Add((string)reader["TABLE_NAME"]);
    }

    return tableNames;
}


l
转自 lauxjpn 4 年前
2,551

本文写于 2021 年 7 月。

WebBrowser 不能改用 Chrome 内核。

在 .NET Frameworks 和 .NET Core 年代,常用的第三方组件有:WebKit.NET、CefSharp 等。

而并未找到适用于 .NET 5 的成熟的 Chromium 内核的浏览器组件。

不过还是推荐一款微软官方出品的 WebView2,需要安装 Microsoft Edge Canary 才能正常运行。


xoyozo 4 年前
2,793

菜单 - 工具 - 选项 - 项目和解决方案 - ASP.NET Core - “自动生成和刷新”选项

image.png

xoyozo 4 年前
2,654

“Web 部署”方式发布 ASP.NET Core 网站项目可解决发布到本地文件夹再通过 FTP 上传到 IIS 中会遇到的文件被锁定/占用的问题。相对于手动停止网站甚至结束进程来说,Web 部署更为方便。

  1. 服务器管理器 - 添加角色和功能 - 服务器角色 - Web 服务器(IIS) - 管理工具 - 管理服务

    image.png

  2. 安装 Web Deploy

    下载 Web 部署,安装时选择“完整”

    image.png

  3. 在“服务”中设置“Web Management Service”和“Web 部署代理服务”自动启用。(若没有找到“Web 部署代理服务”,检查安装 Web Deploy 时是否勾选全部)

    image.png

  4. IIS 管理器 - 管理服务 - 启用远程连接

    image.pngimage.png

    这里我们不使用 Windows 凭据(本地用户),而使用 IIS 管理器用户。

    端口默认 8172,需要在防火墙中允许该端口。在阿里云 ECS 的安全组规则中添加该端口允许。

    image.pngimage.png

  5. 创建 IIS 管理器用户

    image.png

    打开后右侧添加用户,以“iisWebDeploy”为例

    image.png

  6. 配置 IIS 管理器权限

    选择单个网站

    image.pngimage.png

  7. 给整个网站目录添加 LOCAL SERVICE 的完全控制权限 2023年在 Windows Server 2022 上未设置 LOCAL SERVICE 的权限也能部署成功,所以忽略此步骤!2024年同样在 Windows Server 2022 上未设置 LOCAL SERVICE 部署失败,设置后部署成功。

    image.png

  8. 发布

    image.pngimage.pngimage.pngimage.png

  9. 【建议】设置用户 WDeployAdmin 与 WDeployConfigWriter 的密码永不过期,否则会遇到:在远程计算机上处理请求时出错。



一些已知错误的解决办法:

  • 若遇到已下载的 Microsoft SQL Server 2012 Transact-SQL ScriptDom 签名验证失败,手动下载安装即可:Microsoft SQL Server 2012 SP4 功能包,选择 SqlDom.msi(有两个同名文件,感觉上尺寸较大的应该是 x64,我没有具体对比,安装大的成功了)。

  • 若遇到 SQL Server 2012 SP1 Shared Management Object (x86 / x64) 下载失败,同样点击上面的链接,选择 SharedManagementObjects.msi 下载安装。

  • Web Deploy 安装失败,如果 Web Deploy for Hosting Servers 下载失败,可以尝试安装 Web Deploy without bundled SQL support (last)。

  • 如果是新项目,记得在 IIS 对应的网站中添加“IIS 管理器权限”中添加用户。

  • 连接对话框中的“站点名称”或“网站名”必须与 IIS 中的网站名称一致。

  • 还是无法连接?查看 IIS 管理服务中的 SSL 证书是否过期。

  • Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失败。检查输出窗口了解更多详细信息。尝试在“控制面板-卸载或更改程序”中修复 Microsoft Web Deploy 程序。

xoyozo 4 年前
3,827

命名空间“X.PagedList.Mvc.Core”中不存在类型或命名空间名“Common”(是否缺少程序集引用?)

X.PagedList.Mvc.Core.Common.PagedListRenderOptions

X.PagedList.Web.Common.PagedListRenderOptionsBase

改为

X.PagedList.Web.Common.PagedListRenderOptions

xoyozo 4 年前
2,845