How and what to deploy Angular2 to IIS

后端 未结 7 2080
野趣味
野趣味 2020-12-24 03:30

Take for instance angular2-quickstart. What files need to be deployed and what settings need to be set to launch this web app from IIS?

This is a Typescript tutoria

相关标签:
7条回答
  • 2020-12-24 04:11

    Angular 2 routing (with hash) will work without any issue on IIS. Just create default URL rewrite rule which will redirect all the requests to index.html file of your angular app. Rule will redirect all requests to index.html except for required js files and actual angular app urls (i.e. index.html or index.html#/{route-value}.

    EX:

    <rules>
      <rule name="Default">
        <match url="(.* ).js|index.html(.*)" negate="true" />
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
    

    Angular 2 routing (without hash) will not work with IIS. In case of pure HTML application IIS will be routing the incoming request and it will redirect the request to error page if such page does not exist at that location.

    In case of .Net MVC application you can create a default route to handle all the incoming request url's and redirect it to your angular index view.

    Ex Route for MVC application:

    routes.MapRoute(
        name: "Angular",
                url: "{*url}",
               defaults: new { controller = "Home", action = "Index", id =     UrlParameter.Optional },
               constraints: new { url = new AppFeatureUrlConstraint() }
    
    
    public class AppFeatureUrlConstraint : IRouteConstraint
        {
            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
            if (values[parameterName] != null)
            {
                var url = values[parameterName].ToString();
                if (url.StartsWith("angular/", StringComparison.InvariantCultureIgnoreCase))
                    return true;
                else
                    return false;
            }
            return false;
            }
        }
    
    0 讨论(0)
提交回复
热议问题