OWIN send static file for multiple routes

后端 未结 2 1118
北海茫月
北海茫月 2021-02-02 17:59

I\'m making a SPA which sits on top of ASP.Net WebAPI. I\'m waiting to use HTML5 history rather than #/ for history routing but that poses a problem for deep linkin

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 18:50

    I encountered a similar issue using Angular js, and used a slightly different approach to resolve the issue.

    We used Owin to map a route to the entry point of the SPA (index.html). This allows you to access the SPA and navigate to the different pages. However, if you ever refreshed the page you would get a 404. Essentially, AngularJS's routing and Owin/Katana's routing were stepping on each others toes.

    I resolved the issue by creating a custom DelegatingHandler. This delegating handler is used whenever Owin/Katana is unable to find a route matching the request (404).

    public class CustomDelegatingHandler : DelegatingHandler
    {
        protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Task response = base.SendAsync(request, cancellationToken);
            if (response.Result.StatusCode == HttpStatusCode.NotFound)
            {
                response.Result.Content = new StringContent(File.ReadAllText(@"index.html"));
                response.Result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                response.Result.StatusCode = HttpStatusCode.OK;
            }
            return response;
        }
    }
    

    The snippet above returns index.html, the entry point of the SPA, when we are unable to find a page matching the request.

    In order to use this delegating handler, you must add the following line to your HttpConfiguration() when starting the Owin host:

            var httpConfig = new HttpConfiguration();
            httpConfig.MessageHandlers.Add(new CustomDelegatingHandler());
    

    In short, I have a default route that maps to the SPA, and any unrecognized route will go through the DelegatingHandler and serve the same SPA. We do not modify the Request.Path, allowing the SPA to route the request to the proper page.

提交回复
热议问题