Last post Apr 25, 2020 03:36 AM by mywatermelon
Member
53 Points
246 Posts
Apr 24, 2020 11:04 AM|mywatermelon|LINK
I want to match the .html suffix.
For example, the default index page is
https://localhost:44354/home/index
I want to match this URL also
https://localhost:44354/home/index.html
I added these codes in startup.cs
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}.html/{id?}"); });
After the program ran, it reports this error:
An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches:
WebApplication1.Controllers.HomeController.Index (WebApplication1) WebApplication1.Controllers.HomeController.Privacy (WebApplication1) WebApplication1.Controllers.HomeController.Error (WebApplication1) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
Stack Query Cookies Headers Routing AmbiguousMatchException: The request matched multiple endpoints. Matches: WebApplication1.Controllers.HomeController.Index (WebApplication1) WebApplication1.Controllers.HomeController.Privacy (WebApplication1) WebApplication1.Controllers.HomeController.Error (WebApplication1) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState) Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState) Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext) Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext) Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.TryServeStaticFile(HttpContext context, string contentType, PathString subPath) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Show raw exception details
Why it matched multiple endpoints? How can I solve it? Thank you.
All-Star
58174 Points
15647 Posts
Apr 24, 2020 03:51 PM|bruce (sqlwork.com)|LINK
because the static file handler matches all *.html also. you have a couple options:
1) you can remove the static file handler, and then write actions to return required static content.
2) rewrite the urls via middleware. just strip the .html suffix. the middleware would need to proceed the static file handler. see:
https://weblog.west-wind.com/posts/2020/Mar/13/Back-to-Basics-Rewriting-a-URL-in-ASPNET-Corehttps://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1
Apr 25, 2020 03:36 AM|mywatermelon|LINK
I tried to found more about the static file handler while there is almost none about it.
Then I read the article you provided and found the official tutorial of Microsoft:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1
It said:
"URL rewriting can reduce the performance of an app. Where feasible, limit the number and complexity of rules."
Well, it seems it is not a good choice.
Finally, I found a strange&stupid way by adding the route of the controller. For example:
[Route("Index.html")] public IActionResult Index() { return View(); }
In this way, I have to add a Route in every action. It is troublesome also. Aha.
</div>
Member
53 Points
246 Posts
Why I can't match the .html suffix in router?
Apr 24, 2020 11:04 AM|mywatermelon|LINK
I want to match the .html suffix.
For example, the default index page is
https://localhost:44354/home/index
I want to match this URL also
https://localhost:44354/home/index.html
I added these codes in startup.cs
After the program ran, it reports this error:
An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:
WebApplication1.Controllers.HomeController.Index (WebApplication1)
WebApplication1.Controllers.HomeController.Privacy (WebApplication1)
WebApplication1.Controllers.HomeController.Error (WebApplication1)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
Stack Query Cookies Headers Routing
AmbiguousMatchException: The request matched multiple endpoints. Matches: WebApplication1.Controllers.HomeController.Index (WebApplication1) WebApplication1.Controllers.HomeController.Privacy (WebApplication1) WebApplication1.Controllers.HomeController.Error (WebApplication1)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState)
Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.TryServeStaticFile(HttpContext context, string contentType, PathString subPath)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Show raw exception details
Why it matched multiple endpoints? How can I solve it? Thank you.
All-Star
58174 Points
15647 Posts
Re: Why I can't match the .html suffix in router?
Apr 24, 2020 03:51 PM|bruce (sqlwork.com)|LINK
because the static file handler matches all *.html also. you have a couple options:
1) you can remove the static file handler, and then write actions to return required static content.
2) rewrite the urls via middleware. just strip the .html suffix. the middleware would need to proceed the static file handler. see:
https://weblog.west-wind.com/posts/2020/Mar/13/Back-to-Basics-Rewriting-a-URL-in-ASPNET-Corehttps://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1
Member
53 Points
246 Posts
Re: Why I can't match the .html suffix in router?
Apr 25, 2020 03:36 AM|mywatermelon|LINK
I tried to found more about the static file handler while there is almost none about it.
Then I read the article you provided and found the official tutorial of Microsoft:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1
It said:
"URL rewriting can reduce the performance of an app. Where feasible, limit the number and complexity of rules."
Well, it seems it is not a good choice.
Finally, I found a strange&stupid way by adding the route of the controller. For example:
In this way, I have to add a Route in every action. It is troublesome also. Aha.
</div>