2016-09-08 2 views
0

Ich habe ein neues Webprojekt mit .net-Core erstellt. Der Standard-Web-Config wie folgt aussieht:Hinzufügen von Rewrite-Regeln zu web.config .net core

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <!-- 
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 
    --> 

    <system.webServer> 
    <handlers> 
     <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> 
    </handlers> 
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> 
    </system.webServer> 
</configuration> 

Wenn ich diese Konfigurationsdatei öffnen erhalte ich eine Warnung besagt:

Das Element 'system.webServer' hat ungültiges Kind-Element 'aspNetCore'.

Aber ich lasse mich bauen und mein Projekt laufen. Mein Projekt ist eine eckige Anwendung, also möchte ich html5mode hinzufügen. Um dies zu tun, sollte ich in der Lage sein, meine Rewrite-Regeln hinzufügen:

<rewrite> 
    <rules> 
    <!-- AngularJS Html5model --> 
    <rule name="AngularJS Html5model" stopProcessing="true"> 
     <match url=".*" /> 
     <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
     </conditions> 
     <action type="Rewrite" url="/index.html" /> 
    </rule> 
    </rules> 
</rewrite> 

Das Problem ist, sobald ich meine Regeln meiner web.config-Datei hinzufügen, wie folgt aus:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <!-- 
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 
    --> 

    <system.webServer> 
    <rewrite> 
     <rules> 
     <!-- AngularJS Html5model --> 
     <rule name="AngularJS Html5model" stopProcessing="true"> 
      <match url=".*" /> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="/index.html" /> 
     </rule> 
     </rules> 
    </rewrite> 
    <handlers> 
     <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> 
    </handlers> 
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> 
    </system.webServer> 
</configuration> 

Meine Anwendung funktioniert nicht mehr. Es ist, als könnte es nicht herausfinden, was eine statische Datei ist oder nicht. Eine statische Datei kann jetzt nicht gefunden werden. Mit Blick auf die Regel, es statisch, dass jede Datei an "/" umgeleitet werden soll. Kann jemand dieses Problem beheben?

Antwort

1

Es scheint, dass this is a known error und wird nicht bis Ende dieses Jahres behoben werden. Es gibt jedoch eine Problemumgehung. Ich brauchte eine RouteBuilder meiner Startup Klasse hinzuzufügen:

Dies allein wird nicht funktionieren
var routeBuilder = new RouteBuilder(app); 

routeBuilder.MapGet("{*anything}", context => 
{ 
    context.Response.Redirect("/index.html"); 
    return Task.FromResult(0); 
}); 

var routes = routeBuilder.Build(); 
app.UseRouter(routes); 

, müssen Sie die hinzuzufügen:

services.AddRouting(); 

den ConfigureServices Verfahren . hier ist meine Startup Klasse in ihrer Gesamtheit:

public class Startup 
{ 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddRouting(); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     app.UseDefaultFiles(); 
     app.UseStaticFiles(); 

     // Set up our images 
     app.UseStaticFiles(new StaticFileOptions() 
     { 
      FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"images")), 
      RequestPath = new PathString("/images") 
     }); 

     var routeBuilder = new RouteBuilder(app); 

     routeBuilder.MapGet("{*anything}", context => 
     { 
      context.Response.Redirect("/index.html"); 
      return Task.FromResult(0); 
     }); 

     var routes = routeBuilder.Build(); 
     app.UseRouter(routes); 
    } 
} 
Verwandte Themen