TrilonIO / aspnetcore-angular-universal

ASP.NET Core & Angular Universal advanced starter - PWA w/ server-side rendering for SEO, Bootstrap, i18n internationalization, TypeScript, unit testing, WebAPI REST setup, SignalR, Swagger docs, and more! By @TrilonIO
https://www.trilon.io
MIT License
1.46k stars 433 forks source link

Rewrite Rules #537

Open curlyfro opened 6 years ago

curlyfro commented 6 years ago

does anyone know how to setup rewrite rules?

curlyfro commented 6 years ago

i need to create a serverside mvc route to dynamically create and serve PDFs. in mvc 5 i'd define my rewrite rules in web.config. how do you do this is .netcore v2, particularly this project?

Flood commented 6 years ago

Just create a file rewrite-rules.xml in the root directory.

Edit startup.cs to include:

using Microsoft.AspNetCore.Rewrite;

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    ...
   app.UseRewriter(new RewriteOptions().AddIISUrlRewrite(env.ContentRootFileProvider, "rewrite-rules.xml"));
}

And your rewrite-rules.xml should look like this:

<rewrite>
    <rules>
        <clear />

        <rule name="Redirect to https" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}?{QUERY_STRING}" redirectType="Permanent" appendQueryString="false" />
        </rule> 

    </rules>
</rewrite>