public void Configure(IApplicationBuilder app) { //We are building a url template from scratch, segment by segemtn, oldskool var segment = new TemplateSegment(); segment.Parts.Add(TemplatePart.CreateLiteral("page")); var segment2 = new TemplateSegment(); segment2.Parts.Add( TemplatePart.CreateParameter("title", isCatchAll: true, isOptional: true, defaultValue: null, inlineConstraints: new InlineConstraint[] {}) ); var segments = new TemplateSegment [] { segment, segment2 }; var template = new RouteTemplate("page", segments.ToList()); var templateMatcher = new TemplateMatcher(template, new RouteValueDictionary()); app.Use(async(context, next) => { await context.Response.WriteAsync("We are using two segments, one with Literal Template Part ('page') and the other with Parameter Template Part ('title')"); await context.Response.WriteAsync("\n\n"); await next.Invoke(); }); app.Use(async(context, next) => { var path1 = "/page/what"; var routeData = new RouteValueDictionary();//This dictionary will be populated by the parameter template part (in this case "title") var isMatch1 = templateMatcher.TryMatch(path1, routeData); await context.Response.WriteAsync($"{path1} is match? {isMatch1} => route data value for 'title' is {routeData["title"]} \n"); await next.Invoke(); }); app.Use(async(context, next) => { var path1 = "/page/the/old/man/and/the/sea"; var routeData = new RouteValueDictionary();//This dictionary will be populated by the parameter template part (in this case "title") var isMatch1 = templateMatcher.TryMatch(path1, routeData); await context.Response.WriteAsync($"{path1} is match? {isMatch1} => route data value for 'title' is {routeData["title"]} \n"); await next.Invoke(); }); app.Run(async context => { await context.Response.WriteAsync(""); }); }
public void Configure(IApplicationBuilder app) { //We are building a url template from scratch, segment by segemtn, oldskool var segment = new TemplateSegment(); segment.Parts.Add(TemplatePart.CreateLiteral("page")); var segment2 = new TemplateSegment(); segment2.Parts.Add(TemplatePart.CreateParameter("id", isCatchAll: false, isOptional: false, defaultValue: null, inlineConstraints: new InlineConstraint[] { new InlineConstraint("int") })); var segments = new TemplateSegment[] { segment, segment2 }; var template = new RouteTemplate("page", segments.ToList()); var templateMatcher = new TemplateMatcher(template, new RouteValueDictionary()); app.Use(async(context, next) => { await context.Response.WriteAsync("We are using one segment with two parts, one Literal Template Part ('page') and the other with Parameter Template Part ('id')."); await context.Response.WriteAsync("It is the equivalent of /page/{id:int}"); await context.Response.WriteAsync("\n\n"); await next.Invoke(); }); app.Use(async(context, next) => { var path1 = "/page/10"; var routeData = new RouteValueDictionary();//This dictionary will be populated by the parameter template part (in this case "title") var isMatch1 = templateMatcher.TryMatch(path1, routeData); await context.Response.WriteAsync($"{path1} is match? {isMatch1} => route data value for 'id' is {routeData["id"]} \n"); await next.Invoke(); }); app.Use(async(context, next) => { var path = "/page/a"; var routeData = new RouteValueDictionary();//This dictionary will be populated by the parameter template part (in this case "title") var isMatch1 = templateMatcher.TryMatch(path, routeData); await context.Response.WriteAsync($"{path} is match? {isMatch1} - as you can see TemplateMatcher does not give a damn about InlineConstraint. It is by design. \n"); }); }
public void Configure(IApplicationBuilder app) { var apiSegment = new TemplateSegment(); apiSegment.Parts.Add(TemplatePart.CreateLiteral("api")); var serviceNameSegment = new TemplateSegment(); serviceNameSegment.Parts.Add( TemplatePart.CreateParameter("serviceName", isCatchAll: false, isOptional: true, defaultValue: null, inlineConstraints: new InlineConstraint[] { }) ); var segments = new TemplateSegment[] { apiSegment, serviceNameSegment }; var routeTemplate = new RouteTemplate("default", segments.ToList()); var templateMatcher = new TemplateMatcher(routeTemplate, new RouteValueDictionary()); app.Use(async(context, next) => { context.Response.Headers.Add("Content-type", "text/html"); var requestPath = context.Request.Path; var routeData = new RouteValueDictionary(); var isMatch = templateMatcher.TryMatch(requestPath, routeData); await context.Response.WriteAsync($"Request Path is <i>{requestPath}</i><br/>Match state is <b>{isMatch}</b><br/>Requested service name is {routeData["serviceName"]}"); await next.Invoke(); }); app.Run(async context => { await context.Response.WriteAsync(""); }); }
public void Configure(IApplicationBuilder app) { //We are building a url template from scratch, segment by segment, oldskool var segment = new TemplateSegment(); segment.Parts.Add( TemplatePart.CreateLiteral("hello") ); var segment2 = new TemplateSegment(); segment2.Parts.Add( TemplatePart.CreateLiteral("world") ); var segments = new TemplateSegment [] { segment, segment2 }; var template = new RouteTemplate("hello", segments.ToList()); var templateMatcher = new TemplateMatcher(template, new RouteValueDictionary()); app.Use(async(context, next) => { await context.Response.WriteAsync("We are building routing from scratch using a template segment consisted of two parts: 'hello' and 'world'.\n\n"); await next.Invoke(); }); app.Use(async(context, next) => { var path1 = "hello/world"; try { var isMatch1 = templateMatcher.TryMatch(path1, new RouteValueDictionary()); await context.Response.WriteAsync($"{path1} is match? {isMatch1}\n"); } catch (Exception ex) { await context.Response.WriteAsync($"Oops {path1}: {ex?.Message}\n\n"); } finally { await next.Invoke(); } }); app.Use(async(context, next) => { var path1 = "/hello/world"; var isMatch1 = templateMatcher.TryMatch(path1, new RouteValueDictionary()); await context.Response.WriteAsync($"{path1} is match? {isMatch1}\n"); await next.Invoke(); }); app.Use(async(context, next) => { var path1 = "/hello/"; var isMatch1 = templateMatcher.TryMatch(path1, new RouteValueDictionary()); await context.Response.WriteAsync($"{path1} is match? {isMatch1}\n"); await next.Invoke(); }); app.Run(async context => { await context.Response.WriteAsync(""); }); }