【问题标题】:asp.net mvc hosting angular app with html5mode and routing带有 html5mode 和路由的 asp.net mvc 托管 Angular 应用程序
【发布时间】:2014-11-02 01:24:33
【问题描述】:

好的,所以我在 asp.net mvc 5 中托管了一个 angularjs,并在我的 angular 应用程序中使用 html5mode(true) 来消除 url 中的所有哈希符号。 一切都很好,但是,我目前的设置如下所示:

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 容器的视图,并且 Angular 应用程序现在处于活动状态并且正在工作提供路线

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

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

我可以接受现在的 url 格式,但不需要提供可选参数,例如 http://url/app/

此外,它只是一个 Angular 应用程序,除了 index.cshtml 包装器之外没有其他 mvc 视图。

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

【问题讨论】:

  • 您使用的是什么版本的 IIS?是 IIS 6 吗?
  • 我不确定,我正在部署到 windows azure,我认为他们没有使用这么旧的版本...我假设 IIS 8。你认为这可能是一个 IIS 问题?跨度>
  • 是的,它是第 8 版

标签: asp.net html angularjs routing


【解决方案1】:

尝试将其添加到您的 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>

我希望这会有所帮助。

【讨论】:

  • @Josef 很抱歉听到这个消息。我已经编辑了我的答案,请看看新的建议,如果它有效,请告诉我,谢谢!
  • 我之前尝试过,没有成功。打开 rullAllManagedModules 再次尝试,仍然没有成功。我确实向发布类似问题的人发送了一封邮件,询问他的路由......我尝试了很多东西,但没有任何效果。我的 Index 操作没有什么奇怪的,我在其他非角度应用程序中也有类似的设置,而且效果很好......
  • 我可以提到我在 Ng 文件夹中有我的布局,在我的布局中我将基本 href 设置为 ...不知道是不是这样导致问题
  • @Josef 您可以尝试将其发布到您的本地 IIS 服务器吗?只是为了确保 Azure 配置没有问题。
  • 我也试过了,通过visual studio和IIS express同样的错误。当我添加另一个控制器并设置一个可以工作的默认路由时。 /Init 带我到 /Init/Index
【解决方案2】:

这是我的解决方案。我正在使用 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");
}

【讨论】:

  • 除了index.html之外,你还提供静态文件吗?
【解决方案3】:

好的,我开始工作了。

通过这样做:

  1. 彻底删除“app”路由,使用标准路由。
  2. 添加重写规则
  3. 从布局中删除基本 href

哇哦,我关闭了捆绑和缩小功能,这实际上是它最终起作用的原因。当我打开它时,我得到一个角度错误。

老实说,我以为我尝试了 10 次都没有成功。当我删除基本 href 时,它开始显示出工作迹象。

【讨论】:

  • 非常感谢您的参与!
  • Josep,原来是 asp.net 的缩小和捆绑服务导致错误之一。
  • 添加重写规则,删除基本 href(如本答案中所述)并在状态配置中包含 templateUrl 的文件扩展名对我有用。
【解决方案4】:

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

<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>

【讨论】:

    【解决方案5】:

    对于我的 ASP.NET 5 RC 站点 - 有 WebAPI 但没有 MVC - 解决方案是在 wwwroot\web.config 的 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>
    

    【讨论】:

      【解决方案6】:

      我面临同样的问题,我在 layout.cshtml 中设置了基本 href,设置了 webconfig 重写规则,在加载应用程序时,在捆绑配置中,我在启动应用程序时设置了所有脚本和 css,它没有得到加载。意外的语法错误

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 2014-06-18
        • 2014-11-18
        • 2020-01-19
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        相关资源
        最近更新 更多