IIS部署联调修复:OWIN禁用+程序集绑定+SPA路由回退+错误详情输出

- Web.config: 禁用OWIN自动发现(owin:AutomaticAppStartup=false),添加MySqlConnector依赖的程序集绑定重定向
- Global.asax.cs: Application_BeginRequest实现/admin/路径的SPA路由回退(无需URL Rewrite模块)
- GlobalExceptionFilter: 输出完整异常堆栈+InnerException,便于联调定位问题
- request.ts: 错误提示直接显示后端返回的message,不再覆盖为固定文案
- vite.config.ts: base设为/admin/,静态资源路径正确指向/admin/assets/
main
haoliang 1 week ago
parent 126154fc7b
commit b28a89f263

@ -42,15 +42,8 @@ service.interceptors.response.use(
return Promise.reject(new Error(res.message))
}
// 其他业务错误
const errorMessages: Record<number, string> = {
40001: res.message || '参数校验失败',
40002: res.message || '资源不存在',
40003: res.message || '资源已存在',
50001: '服务器错误',
50002: '采集服务未响应',
}
const msg = errorMessages[res.code] || res.message || '请求失败'
// 其他业务错误:优先显示后端返回的消息,便于定位问题
const msg = res.message || '请求失败'
ElMessage.error(msg)
return Promise.reject(new Error(msg))
},

@ -5,6 +5,8 @@ import path from 'path'
// https://vite.dev/config/
export default defineConfig({
// 部署到 /admin/ 子路径,静态资源路径前缀
base: '/admin/',
plugins: [
vue(),
viteMockPlugin({

@ -29,11 +29,26 @@ namespace CncWebApi.Filters
}
else
{
// 未预期异常:记录日志,返回通用错误
// TODO: 接入 log4net 记录完整异常堆栈
// 未预期异常:返回详细错误信息用于诊断(生产环境应改为记录日志)
var ex = context.Exception;
var sb = new System.Text.StringBuilder();
sb.AppendLine($"[{ex.GetType().FullName}] {ex.Message}");
// 堆栈跟踪
if (ex.StackTrace != null) sb.AppendLine(ex.StackTrace);
// InnerException 递归
var inner = ex.InnerException;
int depth = 1;
while (inner != null && depth <= 5)
{
sb.AppendLine($"--- InnerException ({depth}) ---");
sb.AppendLine($"[{inner.GetType().FullName}] {inner.Message}");
if (inner.StackTrace != null) sb.AppendLine(inner.StackTrace);
inner = inner.InnerException;
depth++;
}
var response = ApiResponse<object>.Fail(
CncModels.Constants.ErrorCode.InternalError,
"服务器内部错误");
sb.ToString());
context.Response = context.Request.CreateResponse(
HttpStatusCode.InternalServerError, response);
}

@ -18,5 +18,35 @@ namespace CncWebApi
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
/// <summary>
/// SPA 路由回退:/admin/ 下的非文件请求重写到 index.html
/// 用于支持前端 Vue Router 的 HTML5 History 模式
/// </summary>
protected void Application_BeginRequest(object sender, EventArgs e)
{
var path = Request.Url.AbsolutePath;
// 只处理 /admin/ 开头的路径
if (path.StartsWith("/admin/", StringComparison.OrdinalIgnoreCase) ||
path.Equals("/admin", StringComparison.OrdinalIgnoreCase))
{
// 排除静态资源文件(有扩展名的)
var ext = System.IO.Path.GetExtension(path);
if (string.IsNullOrEmpty(ext))
{
// SPA 路由:重写到 /admin/index.html
var physicalFile = Server.MapPath(path);
if (!System.IO.File.Exists(physicalFile))
{
var indexPath = Server.MapPath("/admin/index.html");
if (System.IO.File.Exists(indexPath))
{
Context.RewritePath("/admin/index.html");
}
}
}
}
}
}
}

@ -14,6 +14,8 @@
</connectionStrings>
<appSettings>
<!-- 禁用 OWIN 自动发现(本项目使用 Global.asax + WebApiConfig -->
<add key="owin:AutomaticAppStartup" value="false" />
<!-- JWT认证密钥生产环境请更换 -->
<add key="JwtSecret" value="CncDataSystem_2026_SecretKey_For_Jwt_Token_Generation_Min32Chars" />
<!-- Token过期时间小时 -->
@ -23,6 +25,8 @@
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<!-- 临时开启详细错误用于联调 -->
<customErrors mode="Off" />
</system.web>
<system.webServer>
@ -40,6 +44,23 @@
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<!-- MySqlConnector 依赖的程序集绑定重定向 -->
<dependentAssembly>
<assemblyIdentity name="System.Memory" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Loading…
Cancel
Save