Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
What version of IIS are you using? Is it IIS 6? –  Josep Sep 8 '14 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? –  Josef Sep 8 '14 at 18:52
    
Yupp, it is version 8 –  Josef Sep 8 '14 at 19:01

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
That did not work :/ Thanks –  Josef Sep 8 '14 at 19:07
    
@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! –  Josep Sep 8 '14 at 19:24
    
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... –  Josef Sep 8 '14 at 19:30
    
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 Sep 8 '14 at 19:31
    
@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. –  Josep Sep 8 '14 at 19:35

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.

share|improve this answer
    
S**t happens! Glad to know that it's working now ;) –  Josep Sep 9 '14 at 18:37
    
Thanks alot for your engagement! –  Josef Sep 9 '14 at 18:39
    
Josep, it turned out to be the minification and bundling service of asp.net causing one of the errors. –  Josef Sep 9 '14 at 19:36

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("~/yourstartpage.html", "text/html");
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.