asp.net mvc hosting angular app with html5mode and routing

流过昼夜 提交于 2019-11-28 07:47:16

Try adding this in your web.config sytem.webserver settings.

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

EDIT:

Try changing your RouteConfig.cs, like this:

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

EDIT2:

I had completely forgoten about this question, but now I just realized that maybe the problem is that you haven't configured your IIS Server to work with Html5Mode, have a look at this: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

Concretelly this part:

Azure IIS Rewrites:

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

I hope that this helps.

This is my solution. I am using ASP.NET MVC + Web API. ASP.NET MVC always returns the same HTML page for any URL, so AngularJS can take over in $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");
}

Alright, I got it to work.

By doing this:

  1. Remove the "app" route completely, and use the standard route.
  2. Add the rewrite rule
  3. Remove the base href from the layout

Ohh wow, I turned off the bundling and minification, that was actually what made it work in the end. When I turn it on I get an angular error.

I honestly thought I tried this like 10 times without success. It started to show signs of working when I removed the base href.

Old question but still valid, this is my solution :

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

With my ASP.NET 5 RC site - has WebAPI but not MVC - the solution was to up a default rewrite rule in wwwroot\web.config's system.webserver tag:

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

I am facing the same problem, i have set the base href in layout.cshtml ,put the webconfig rewrite rule, when loading the app,In bundle config i have set the all the scripts and css when launch the app ,its not get loaded . unexpected syntax error < is displayed in console.

when i remove the rule and base href and location provider is false , it working fine .

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!