2016-09-08 29 views
0

我已经使用.net核心创建了一个新的Web项目。 默认web配置是这样的:无法将重写规则添加到web.config .net核心

<?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> 

当我打开这个配置文件,我得到一个警告,指出:

元素“system.webServer具有无效子元素“aspNetCore”。

但它让我建立并运行我的项目。 我的项目是一个角度应用程序,所以我想添加html5mode。 要做到这一点,我应该可以加我重写规则:

<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> 

的问题是,当我加入我的规则,以我的web.config文件中,就像这样:

<?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> 

我的申请停止工作。这就像它无法弄清什么是静态文件或不是。现在找不到任何静态文件。 看规则,它会静态地将任何文件重定向到“/”。 有谁知道如何解决这个问题?

回答

1

看来,this is a known error,并将不会固定,直到今年年底。尽管有一个解决方法。 我需要一个RouteBuilder添加到我的启动类:

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

这不会单独工作,你需要将补充:

services.AddRouting(); 

ConfigureServices方法。 这里是我的启动类的全部内容:

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