8

Alright, so I am hosting an angularjs inside asp.net mvc 5 and are using the html5mode(true) in my angular app to get rid of all hash signs in the url. Everything works great, however, with my current setup that looks like this:

RouteConfig.cs:

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

So that when I navigate to http://url/app/myangularroute my Ng/Index action returns the view which contains the ng-view container and the angular app is now active and working with the route provided

Now, my problem here is, when I navigate to http://url/app/ it returns a dir listning not allowed error which I cannot understand. Shouldn't my index action be returned as the angular parameter is set to optional?

And can I somehow avoid the "app" completely and still get my angular app to work? I have tried some rewrite rules but that gives me alot of errors because I am making use of the mvc bundling and minification functionality.

I could live with the url being the format it currently is but without the need to provide the optional parameter, like http://url/app/

Also, it's only an angular app, no other mvc view than the index.cshtml wrapper.

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

3
  • What version of IIS are you using? Is it IIS 6? Commented Sep 8, 2014 at 18:51
  • I am not sure, I am deploying to windows azure, I don't think they are using such old version...I assume IIS 8. You think this could be a IIS issue? Commented Sep 8, 2014 at 18:52
  • Yupp, it is version 8 Commented Sep 8, 2014 at 19:01

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

9 Comments

@Josef Sorry to hear that. I've edited my answer, please have a look at new suggestion and let me know if it works, thanks!
I have tried that earlier, no success. Tried it again with the rullAllManagedModules turned on, still no success. I did send an mail to the guy posted the similar question asking for his routing...I have tried a lot of things and nothing works. There is nothing strange with my Index action, and I have similar setups in other non-angular apps and it works great...
I could mention that I have my layout in the Ng folder and in my layout i set the base href to <base href="/app/" />...don't know if that is causing the problem
@Josef Could you try publishing this into your local IIS server? Just to make sure that it's not a problem with the Azure configuration.
I have tried that as well, same error through visual studio and IIS express. When I add another controller and set up a default route that one works. /Init takes me to /Init/Index
|
7

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");
}

1 Comment

Do you serve up any static files besides index.html?
2

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.

3 Comments

Thanks alot for your engagement!
Josep, it turned out to be the minification and bundling service of asp.net causing one of the errors.
Adding the rewrite rule, removing the base href (as mentioned in this answer) and including the file extension for the templateUrl in the states configuration worked for me.
1

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>

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.