2013-11-15 4 views
1

저는 MVC와 라우팅에 비교적 익숙하기 때문에 나와 어울리지 않습니다. 나는이 문제를 해결하기 위해 며칠 동안 노력해 왔기 때문에 잘못된 문제를 풀려고하거나 실제로 검색 할 용어를 모른다.SPA와 Web Api를 사용하는 비 Api 라우팅

저는 AngularJS와 Web Api (4.5)를 사용하여 SPA를 만들고 있습니다. 내 의견은 모두 고객 측면입니다. 따라서 서버는 URI에 나타날 수있는 가능한 모든 경로를 알지 못합니다. 이것은 사용자가 브라우저 컨트롤 (back, forward, refresh, history) 중 하나를 사용하고 서버가 요청 된 URI를 라우트하려고 시도 할 때까지는 문제가되지 않습니다.

이 문제를 해결하기 위해 모든 비 API 경로를 애플리케이션 루트에 매핑하는 것에 중점을 두었습니다.

MapHttpRoute에 대한 올바른 구문을 알고 컨트롤러 (API)를 참조하지 않는 경로를 선택하거나이 문제를 처리 할 수있는 더 나은 방법이 있는지 알고 싶습니다.

public static void Register(HttpConfiguration config) 
{ 
    config.MapHttpAttributeRoutes();    

    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
    // 
    // This doesn't work 
    // 
    config.Routes.MapHttpRoute(
     name: "Default", 
     routeTemplate: "{*catchall}" 
    ); 
} 
 
    This XML file does not appear to have any style information associated with it. The document tree is shown below. 
    
    
    No HTTP resource was found that matches the request URI 'http://localhost:9234/login'. 
    
    
    No route providing a controller name was found to match request URI 'http://localhost:9234/login' 
    
    
는 해결, 당신이

답변

0

안 좋은 생각 감사

config.Routes.MapHttpRoute(
      name: "Default", 
      routeTemplate: "{*catchall}", 
      defaults: new { controller = "Spa"} 
      ); 

[AllowAnonymous] 
public sealed class SpaController : ApiController 
{ 
    public HttpResponseMessage Get() 
    { 
     var indexHtml = HostingEnvironment.MapPath("~/index.html"); 

     var stringContent = new StreamContent(File.OpenRead(indexHtml)); 
     var response = new HttpResponseMessage(HttpStatusCode.OK) {Content = stringContent}; 

     return response; 
    } 
}