c# - 如何将 Web API 以外的所有内容路由到/index.html

标签 c# asp.net asp.net-mvc asp.net-mvc-4 angularjs

我一直在使用 Web API 在 ASP.NET MVC 内部开发一个 AngularJS 项目。它工作得很好,除非您尝试直接转到 Angular 路由 URL 或刷新页面。我认为我可以使用 MVC 的路由引擎 来处理这件事,而不是胡闹服务器配置。

当前的 WebAPI 配置:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" }
        );

        config.Routes.MapHttpRoute(
            name: "ApiWithActionAndName",
            routeTemplate: "api/{controller}/{action}/{name}",
            defaults: null,
            constraints: new { name = @"^[a-z]+$" }
        );

        config.Routes.MapHttpRoute(
            name: "ApiWithAction",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" }
        );
    }
}

当前路由配置:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute(""); //Allow index.html to load
        routes.IgnoreRoute("partials/*"); 
        routes.IgnoreRoute("assets/*");
    }
}

当前的 Global.asax.cs:

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        var formatters = GlobalConfiguration.Configuration.Formatters;
        formatters.Remove(formatters.XmlFormatter);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            PreserveReferencesHandling = PreserveReferencesHandling.None,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
    }
}

目标:

/api/* 继续转到 WebAPI,/partials/ 和/assets/ 都转到文件系统,绝对其他所有内容都路由到/index.html,这是我的 Angular 单例页面应用。

--编辑--

我似乎已经开始工作了。将此添加到 BOTTOM OF RouteConfig.cs:

 routes.MapPageRoute("Default", "{*anything}", "~/index.html");

对根 web.config 的更改:

<system.web>
...
  <compilation debug="true" targetFramework="4.5.1">
    <buildProviders>
      ...
      <add extension=".html" type="System.Web.Compilation.PageBuildProvider" /> <!-- Allows for routing everything to ~/index.html -->
      ...
    </buildProviders>
  </compilation>
...
</system.web>

但是,它闻起来像黑客。有更好的方法吗?

最佳答案

使用通配符段:

routes.MapRoute(
    name: "Default",
    url: "{*anything}",
    defaults: new { controller = "Home", action = "Index" }
);

关于c# - 如何将 Web API 以外的所有内容路由到/index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643001/

相关文章:

C#:将用户定义的类型传递给 Oracle 存储过程

c# - 带图像的 Bot 框架提示对话框

c# - 如何声明 C# Web 用户控件但阻止它初始化?

asp.net-mvc - 在/error Controller 中检索HTTP错误详细信息

asp.net-mvc - 如何调试缩小失败。返回未缩小的内容

c# - 注册 Moq 实例时 Autofac 抛出 File Not Found 异常

c# - WPF 中的 SharpDX 渲染

c# - 在 Newtonsoft JSON 中转义引号

c# - 如何禁用 ASP.NET 成员身份提供程序的帐户?

c# - ASP.NET 中的 Windows 时区缩写