2021年3月26日星期五

ASP.NET 5 regex route endpoint for files and non-files

I have this possible urls:

mywebsite/a/b/c.txt  mywebsite/x/t  mywebsite/z  mywebsite/z/i.jpg  

So when the url ends with a file, I need to return 404, otherwise I call the controller. To this I have

endpoints.MapControllerRoute(      name: "default",      pattern: "{*.}",      defaults: new { controller = "Home", action = "Index" });  

And this how I'm trying to check for router files

// endpoints.MapGet("{file:regex(.(css|txt|js|jpg|png|ico|json)$)}", async context =>  // endpoints.MapGet(@"(.*\.)(jpe?g|css|txt|js|png|ico|json)$", async context =>  /* endpoints.MapGet(@"{file:regex((.*\.)(jpe?g|css|txt|js|png|ico|json)$)}", async context =>  {      context.Response.StatusCode = 404;  }); */    endpoints.MapControllerRoute(      name: "File",      // pattern: @"(.*\.)(jpe?g|css|txt|js|png|ico|json)$",      // pattern: @"{controller:(.*\.)(jpe?g|css|txt|js|png|ico|json)$}",      // pattern: "{ssn}",      // constraints: new { ssn = @"(.*\.)(jpe?g|css|txt|js|png|ico|json)$" },      pattern: @".*\.(css|js|gif|jpg)(/.)?",      defaults: new { controller = "NotFound", action = "Index" });  

The problem is that https://localhost:5001/style.css works, but https://localhost:5001/z/style.css it doesn't.

So what could I be missing here?

https://stackoverflow.com/questions/66818867/asp-net-5-regex-route-endpoint-for-files-and-non-files March 26, 2021 at 10:30PM

没有评论:

发表评论