ASP.NET 5 and Angular Routing not working on dnx rc1

最后都变了- 提交于 2019-12-13 07:15:12

问题


Hi I have a project built in ASP.NET 5 (dnxcore50 and dnx451). It is serving the requests for my AngularJS scripts. When I built the project I was using the Microsoft beta 5 dependencies and using a rewrite rule to send all the requests to index.html and so my angular routing would work just fine. But today I had to upgrade to the rc1 dependencies and now my rewrite is not working and I'm just getting a 404 on my route. This new libraries added this weird lines of code to my web.config

  <handlers>
  <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" />

Is there a way how can I set my routing so that it works with angular for example if I go to localhost/shop it will redirect it to index.html and my angular routing will take over.


回答1:


I am on similar setup and I have in Configure in Startup.cs,

    app.UseDefaultFiles();

    app.UseStaticFiles();

    app.UseIISPlatformHandler();

and a rewrite rule in system.webServer in web.config, needed that to be able to have both web api and index.html angular app served:

    <rewrite>
      <rules>
        <!--Redirect selected traffic to index -->
        <rule name="Index Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>

You may need something similar too with your own rules




回答2:


I think there is a better way than using the url rewrite in the routing setup (So that there is no need for the url rewrite at all). E.g. I did this:

    app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "api",
                template: "api/{controller}/{action}");

            routes.MapRoute(
                name: "angular",
                template: "{*url}",
                defaults: new {controller = "Home", action = "Index"},
                constraints: new {url = new DoesNotContainConstraint(".", "api/") });                
        });

This ensures that all /api goes to controllers and everything else goes to the Index. I had to do my own DoesNotContainConstraint to ensure that files and api with wrong url are properly handled, but it's rather easy.




回答3:


If you want to use ASP.NET 5/ASP.NET Core with IIS you need to install HttpPlatformHandler. Here is a nice step by step instruction showing how to install Http Platform Handler, configure IIS and publish an APS.NET Core application to IIS.



来源:https://stackoverflow.com/questions/35444475/asp-net-5-and-angular-routing-not-working-on-dnx-rc1

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