【问题标题】:WebAPI routing - Map all requests to a sub-folderWebAPI 路由 - 将所有请求映射到子文件夹
【发布时间】:2015-06-19 07:11:59
【问题描述】:

我正在尝试做一些非常简单的事情,但我在谷歌和文档中都找不到任何东西。

我有一个空的 Web API 应用程序,带有默认的 WebApiConfig:

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 }
        );
    }
}

我唯一添加的是一个带有 html/js 应用程序的文件夹:

WebApplication
|-- App_Start
|-- Controllers
|-- MySubFolder
|   |-- index.html
|   |-- js
|   |   `-- app.js
|   |-- css
|   |   `-- style.css
`-- Global.asax

我希望将每个不以“api/”开头的请求重定向到 MySubFolder。例如:

  • GET / --> /MySubFolder/index.html
  • GET /js/app.js --> /MySubFolder/js/app.js
  • GET /api/user/ --> UserController(已经使用默认规则)

【问题讨论】:

    标签: c# asp.net asp.net-web-api routing


    【解决方案1】:

    我已经用owin解决了:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var httpConfiguration = new HttpConfiguration();
    
            // Configure Web API Routes:
            // - Enable Attribute Mapping
            // - Enable Default routes at /api.
            httpConfiguration.MapHttpAttributeRoutes();
            httpConfiguration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            app.UseWebApi(httpConfiguration);
    
            // Make ./MySubFolder the default root of the static files in our Web Application.
            app.UseFileServer(new FileServerOptions
            {
                RequestPath = new PathString(string.Empty),
                FileSystem = new PhysicalFileSystem("./MySubFolder"),
                EnableDirectoryBrowsing = true,
            });
    
            app.UseStageMarker(PipelineStage.MapHandler);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多