无法向web.config .net core

时间:2016-09-08 09:56:39

标签: asp.net angularjs asp.net-core

我用.net核心创建了一个新的Web项目。 默认的Web配置如下所示:

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

当我打开此配置文件时,我收到一条警告:

  

元素&#39; system.webServer&#39;具有无效的子元素&#194; aspNetCore&#39;。

但它让我构建并运行我的项目。 我的项目是一个角度应用程序,所以我想添加 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>

我的申请停止工作。它无法弄清楚什么是静态文件。现在无法找到任何静态文件。 查看规则,它会将任何文件重定向到&#34; /&#34;。 有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

似乎this is a known error并且在今年年底之前不会修复。但有一个解决方法。 我需要在 Startup 类中添加 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 方法。 这是我的 Startup 类的全部内容:

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