Middleware for custom urls–part 29
The question was how I can now intercept calls like
www.infovalutar.ro/bnr/2004/10/10/EUR
www.infovalutar.ro/bnr/azi/eur
www.infovalutar.ro/2004/6/4/eur.bnr
http://www.infovalutar.ro/bnr/rss
? There is no controller ! So – middleware to help!
For reading about middleware, please see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.0
For rewriting, please see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.0
Anyway, this is my code:
var service = app.ApplicationServices.GetService<LoadExchangeProviders>();
foreach (var item in service.Banks())
{
app.MapWhen(cnt =>
cnt.Request.Path.Value.StartsWith(“/” + item + “/”, StringComparison.InvariantCultureIgnoreCase)
||
cnt.Request.Path.Value.EndsWith(“.” + item, StringComparison.InvariantCultureIgnoreCase)
, HandleBank);
}
Figures also that previous answer was a string, not an object – changing return type to string
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )
Leave a Reply