2016-02-28 16 views
0

我需要为我的HTML5应用程序的URL重写提供帮助,该应用程序位于IIS上。URL重写影响来自API的数据

在我的应用程序中,我有页面列出学校。学校数据(JSON)来自C#ASP.net API。我通过使用AngularJS($ http.Get)获取数据。

当我单独跑步即没有角度路由时,学校页面正常工作。之后我包括角JS路由和主机在我的本地IIS,所有的工作除了学校网页不错的网页,这是唯一的页面我得到来自API的数据,

我的API网址:

http://localhost/ita_calender_api/ita/values 

学校网页网址:

http://localhost:8080/Schools 

(注意:我的应用程序在8080端口上运行和API是默认值)在Web.config中

URL重写:

<rewrite> 
     <rules> 
      <rule name="AngularJS Routes" stopProcessing="true"> 
      <match url=".*" /> 
      <conditions logicalGrouping="MatchAll"> 
       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
       <add input="{REQUEST_URI}" pattern="ITA_CALENDER_API/(.*)" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="/" /> 
      </rule> 
     </rules> 
     </rewrite> 

当我加载的网址,浏览器,我发现了学校网页,与500

一件事的状态我没有忘了包括XHR响应: 的CORS访问单单我的学校页:

<location path="web/Schools.html"> 
    <system.webServer> 
     <httpProtocol> 
     <customHeaders> 
      <add name="Access-Control-Allow-Origin" value="http://localhost/" /> 
     </customHeaders> 
     </httpProtocol> 
    </system.webServer> 
    </location> 
+0

您在使用ASP的Web API或MVC?你有'App_Start/RouteConfig.cs'或'App_Start/WebApiConfig.cs'来管理你的路线吗? – cl3m

+0

@ cl3m确切 – Sankar

+0

我正在使用'App_Start/WebApiConfig.cs'来配置路由... – Sankar

回答

0

编辑 如果你只是希望重定向从/School/web/schools.html你可以只是做兴田以下:

[Route("~/School")] 
    public ActionResult YourActionThatReturnsSchoolDotHtml() 
    { 
     return View(); 
    } 

或者,你可以做一个简单的RedirectToAction

public ActionResult YourActionThatReturnsSchoolDotHtml() 
    { 
     return View(); 
    } 

    [Route("~/School")] 
    public ActionResult Schools() 
    { 
     return RedirectToRoute(YourActionThatReturnsSchoolDotHtml()); 
    } 

我不知道这是否会帮助你,但如果你正在使用ASP MVC,你可以设置通过App_Start/RouteConfig.cs你的路由文件是这样的:

routes.MapMvcAttributeRoutes(); 
routes.MapRoute(
    name: "Schools", 
    url: "Schools/{*url}", 
    defaults: new {controller = "Schools", action = "Index" } 
); 

,然后在你的SchoolsController.cs

[Route("Schools/{*url}")] 
public ActionResult Index() 
{ 
    return View(); 
} 

只要你也有Views/Schools目录

这将基本上/Schools/* MVC行动每呼叫转接到下index.cshtml文件的angular router

+0

首先,我没有从我的API返回意见。它只返回JSON数据..这是显示在学校页面 – Sankar

+0

通常情况下,Web API返回JSON,你的ASP MVC返回意见 – cl3m

+0

我想我困惑你...我的api只返回JSON数据...因为我是初学者,我不能很好地解释你 – Sankar