You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using CncWebApi.App_Start;
|
|
|
|
namespace CncWebApi
|
|
{
|
|
/// <summary>
|
|
/// Web API 应用程序入口
|
|
/// IIS 启动时自动调用 Application_Start
|
|
/// </summary>
|
|
public class WebApiApplication : HttpApplication
|
|
{
|
|
/// <summary>
|
|
/// 应用启动时执行,注册路由和全局配置
|
|
/// </summary>
|
|
protected void Application_Start()
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|