asp.net mvc使用html5mode和路由托管角度应用程序

时间:2014-09-08 18:29:45

标签: asp.net html5 angularjs routing

好吧,所以我在asp.net mvc 5中托管一个angularjs,并在我的角应用程序中使用html5mode(true)来删除网址中的所有哈希标志。 然而,一切都很好,我目前的设置如下:

RouteConfig.cs:

        routes.MapRoute(
            name: "Default",
            url: "app/{angular}",
            defaults: new { controller = "Ng", action = "Index", angular= UrlParameter.Optional }
        );

因此,当我导航到 http://url/app/myangularroute 时,我的 Ng / Index 操作会返回包含ng-view容器的视图,而角度应用现在处于活动状态并使用提供的路线

现在,我的问题是,当我导航到 http://url/app/ 时,它会返回一个dir listning not allowed错误,这是我无法理解的。不应该返回我的索引操作,因为angular参数设置为optional?

我可以以某种方式完全避开“应用程序”并仍然让我的角度应用程序工作吗?我尝试了一些重写规则,但这给了我很多错误,因为我正在使用mvc捆绑和缩小功能。

我可以将网址作为目前的格式,但不需要提供可选参数,例如 http://url/app/

此外,它只是一个角度应用程序,没有其他mvc视图比index.cshtml包装器。

This guy seems to get it to work, but I can't see his mvc routes

6 个答案:

答案 0 :(得分:16)

尝试在web.config sytem.webserver设置中添加此内容。

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
<system.webServer>

编辑

尝试更改RouteConfig.cs,如下所示:

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

<强> EDIT2:

我完全忘记了这个问题,但现在我才意识到问题可能是您没有将IIS服务器配置为使用Html5Mode,请看一下:https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

具体来说这部分:

Azure IIS重写:

<system.webServer>
  <rewrite>
    <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

我希望这会有所帮助。

答案 1 :(得分:7)

这是我的解决方案。我正在使用ASP.NET MVC + Web API。 ASP.NET MVC总是为任何URL返回相同的HTML页面,因此AngularJS可以接管$ locationProvider.html5Mode(true);

RouteConfig.cs

  routes.MapRoute(
      name: "Default",
      url: "{*anything}",
      defaults: new
      {
        controller = "Home",
        action = "Index",
      }
  );

WebApiConfig.cs

  config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{action}/{id}/{id1}",
          defaults: new
          {
            action = RouteParameter.Optional,
            id = RouteParameter.Optional,
            id1 = RouteParameter.Optional,
          }
      );

HomeController.cs

public ActionResult Index()
{
    return File("~/index.html", "text/html");
}

答案 2 :(得分:2)

好吧,我让它上班了。

通过这样做:

  1. 删除&#34; app&#34;完全路线,并使用标准路线。
  2. 添加重写规则
  3. 从布局中删除基础href
  4. 哇,哇,我关掉了捆绑和缩小,这实际上是最终使它工作的原因。当我打开它时,我得到一个角度误差。

    老实说,我认为我尝试了10次没有成功。当我移除基础href时,它开始显示出工作的迹象。

答案 3 :(得分:1)

旧问题但仍然有效,这是我的解决方案:

<rewrite>
  <rules>
    <rule name="Main Rule" stopProcessing="true">
      <match url=".*"/>
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <!--for local dev enviroment-->
        <add input="{REQUEST_URI}" pattern="^/(__browserLink)" negate="true" />
        <!--if the url does not match a valid file we don't want it to be redirected to the index page-->
        <add input="{REQUEST_URI}" pattern="\.(png|jpg|gif|css|js|html)$" negate="true" />
        <!--web api route-->
        <add input="{REQUEST_URI}" pattern="^/api/" negate="true" />
        <!--ASP.NET Web API Help Page-->
        <add input="{REQUEST_URI}" pattern="^/Help" negate="true" />
        <!--Swagger-->
        <add input="{REQUEST_URI}" pattern="^/apimap" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

答案 4 :(得分:0)

使用我的ASP.NET 5 RC站点 - 有WebAPI而不是MVC - 解决方案是在wwwroot \ web.config&#39; s system.webserver标记中启用默认重写规则:

<rewrite>
  <rules>
    <clear />
    <rule name="API" stopProcessing="true">
      <match url="^(api)(.*)$" />
      <action type="None" />
    </rule>
    <rule name="Main Rule" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

答案 5 :(得分:0)

我面临同样的问题,我在layout.cshtml中设置了基本href,在加载应用程序时设置了webconfig重写规则,在bundle config中我已经设置了启动应用程序时的所有脚本和css,没有加载。意外的语法错误&lt;在控制台中显示。

当我删除规则并且基本href和位置提供程序为false时,它工作正常。

Angular Js Strange issue Ui Router Html5mode enabled page refresh issue in mvc 500 error