using System;
using System.Web;
using System.Web.Http;
using CncWebApi.App_Start;
namespace CncWebApi
{
///
/// Web API 应用程序入口
/// IIS 启动时自动调用 Application_Start
///
public class WebApiApplication : HttpApplication
{
///
/// 应用启动时执行,注册路由和全局配置
///
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
///
/// SPA 路由回退:/admin/ 下的非文件请求重写到 index.html
/// 用于支持前端 Vue Router 的 HTML5 History 模式
///
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");
}
}
}
}
}
}
}