Javascript URL重写会影响来自API的数据

Javascript URL重写会影响来自API的数据,javascript,angularjs,html,iis,url-rewriting,Javascript,Angularjs,Html,Iis,Url Rewriting,我需要一个帮助我的HTML5应用程序的URL重写,它是在IIS托管 在我的应用程序中,我有一个列出学校的页面。学校数据(JSON)来自C#ASP.net API。我使用AngularJS($http.Get)获取数据 当我独自跑步时,即没有角度布线时,学校页面工作正常。在我将Angular JS路由和主机包含在本地IIS中之后,除了学校页面之外,所有页面都运行良好,这是我从API获取数据的唯一页面 我的API URL: http://localhost/ita_calender_api/ita/

我需要一个帮助我的HTML5应用程序的URL重写,它是在IIS托管

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

当我独自跑步时,即没有角度布线时,学校页面工作正常。在我将Angular JS路由和主机包含在本地IIS中之后,除了学校页面之外,所有页面都运行良好,这是我从API获取数据的唯一页面

我的API URL:

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>

有一件事我忘了包括: 仅我的学校的CORS访问页面:

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

编辑 如果您只是想从
/School
重定向到
/web/schools.html
,您可以执行以下操作:

    [Route("~/School")]
    public ActionResult YourActionThatReturnsSchoolDotHtml()
    {
        return View();
    }
或者,您可以执行一个简单的
重定向到操作

    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
目录下还有一个
index.cshtml
文件


这将基本上将每个调用重定向到
/Schools/*
MVC操作到您的
angular router

您使用的是ASP Web API还是MVC?您是否有一个
App\u Start/RouteConfig.cs
App\u Start/WebApiConfig.cs
可以管理您的路由?我正在使用
App\u Start/WebApiConfig.cs
配置路由…您是否在单独的项目中同时使用
MVC
webapi
?您的API url没有端口号:
http://localhost/ita_calender_api/ita/values
。这是个错误吗?如果您使用的是
WebApiConfig.cs
,那么您使用的不是
ASP MVC
,而是
ASP Web Api
,这是不同的。您不能直接从运行在本地主机默认端口上的
WebAPI
@cl3m My MVC API服务和运行在本地主机8080端口上的HTML5应用程序中提供
HTML
文件。但是,当单独运行学校页面时(即)没有路由时,它可以工作。首先,我没有从我的API返回视图。它只返回JSON数据。。通常,WebAPI返回JSON,你的ASP MVC返回VIEW。我想我把你弄糊涂了。。。我的api只返回JSON数据。。。因为我是初学者,我不能很好地解释你是的,对不起,我很困惑。你的
/web/school.html
从哪里来?当我在导航栏上单击schools时,它的路径是
。当('/schools',{templateUrl:'web/schools.html',controller:'mainController'})
这是在角度路由配置中。。。