rewriteoptions - url rewrite http to https




Url rewriting in ASP.NET Core RC2 (2)

I found a working solution here.

Below is the code in my Configure method that fixed the issue for me. But be careful, the declaration order matters.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));

    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
    }

    app.Use(async (context, next) => {
        await next();

        if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) {
            context.Request.Path = "/";
            context.Response.StatusCode = 200;
            await next();
        }
    });

    app.UseDefaultFiles();
    app.UseStaticFiles();

    app.UseMvc(routes => {
        routes.MapRoute("Default", "api/{controller}/{action}/{id?}");
    });
}

How can I accomplish URL rewriting in ASP.NET Core RC2? Migrating from RC1 to RC2 broke my Angular 2 routing when I refresh the page.

I was using a rule like this before in my web.config located in my wwwroot. With RC2 I'm not even sure if I'm supposed to have a web.config in my wwwroot anymore, or if I'm just supposed to have the one in the base of my project.

This is my base web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="true" />
  </system.webServer>
</configuration>

And this is my wwwroot web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <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" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/account/" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

When I refresh an angular 2 route, I get a Status Code: 404; Not Found from ASP.NET


I found solution by Steve Sanderson, seems to work. he wrote extension to RouteBuilder. You can configure it in Setup.cs by calling extension method MapSpaFallbackRoute

https://github.com/aspnet/JavaScriptServices

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
            });